Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Who is afraid of regex?

Who is afraid of regex?

Scheduled Pinned Locked Moved The Lounge
regexcssfunctionalquestion
42 Posts 20 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H honey the codewitch

    I think it depends. I generally agree that complicated regex is mug's game. However, How do you technically, and accurately convey a set of rules around lexical requirements? Such rules must be able to be conveyed to other developers precisely. Such rules must be unambiguous, and testable. Such rules must be absorbable in reasonable amount of time, meaning no poring over RFCs if one can avoid it. Imagine conveying the rules for what constitutes a JSON number You can either say:

    (\-?)(0|[1-9][0-9]*)((\.[0-9]+)?([Ee][\+\-]?[0-9]+)?)

    Which takes some unpacking as you say, but is certainly readable. Or I can give you a page long document of requirements around JSON number parsing. Personally, I can read that quite easily, but that's me. Let me propose something - there is a meaningful subset of regular expressions which are easy to understand, and can fulfill most simple lexical specifications like the above, or say, like an email address, or an url, or any number of small, structured text fragments. It beats the alternative, hands down.

    Real programmers use butterflies

    G Offline
    G Offline
    Gary R Wheeler
    wrote on last edited by
    #27

    To my mind that regex would be okay. It's the thousands of characters, wall-of-text abominations that I object to. I know, that's an example of poor use of regex, but it's the kind of thing you find. Inexperienced folks start using it, and all of a sudden it becomes their favorite toy. A toy that's all sharp edges...

    Software Zen: delete this;

    H 2 Replies Last reply
    0
    • G Gary R Wheeler

      I like using regex for day-to-day, throwaway things. It's especially good for reformatting text. I'm certainly not intimidated by them. That said, I don't think I would ever use one in product code with a long life-span. You must admit that regular expressions tend to be write-only, which is a cardinal sin against those who must maintain the code, including your future selves. Code written very concisely, and regular expressions may be the ultimate in concise, require a lot of mental unpacking during maintenance. Unless you write a ridiculous amount of comments for the expression, it might not be worth it.

      Software Zen: delete this;

      S Offline
      S Offline
      Slacker007
      wrote on last edited by
      #28

      we use regex expressions in production code all the time. almost always validation. phone numbers, emails, web addresses, and other pattern specialized validation. never had any issues. performance has never been an issue and accuracy has never been an issue. most of the uses require zero maintenance after implementation.

      R 1 Reply Last reply
      0
      • G Gary R Wheeler

        To my mind that regex would be okay. It's the thousands of characters, wall-of-text abominations that I object to. I know, that's an example of poor use of regex, but it's the kind of thing you find. Inexperienced folks start using it, and all of a sudden it becomes their favorite toy. A toy that's all sharp edges...

        Software Zen: delete this;

        H Offline
        H Offline
        honey the codewitch
        wrote on last edited by
        #29

        :laugh: I won't argue with that.

        Real programmers use butterflies

        1 Reply Last reply
        0
        • R realJSOP

          I make what could be regarded as "heroic effort" to avoid using regex whenever possible. However, I used it in a recent application because it was the most expedient way to do what I needed.

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

          H Offline
          H Offline
          honey the codewitch
          wrote on last edited by
          #30

          Expedience is how it gets you. Next thing you know, you're hooked. :laugh:

          Real programmers use butterflies

          R 1 Reply Last reply
          0
          • H honey the codewitch

            Expedience is how it gets you. Next thing you know, you're hooked. :laugh:

            Real programmers use butterflies

            R Offline
            R Offline
            realJSOP
            wrote on last edited by
            #31

            I’ve resisted being hooked on its expedience since it’s inception. The way I see it, I will be able to easily make it to retirement (three years) without getting hooked on it.

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            H 1 Reply Last reply
            0
            • G Gary R Wheeler

              To my mind that regex would be okay. It's the thousands of characters, wall-of-text abominations that I object to. I know, that's an example of poor use of regex, but it's the kind of thing you find. Inexperienced folks start using it, and all of a sudden it becomes their favorite toy. A toy that's all sharp edges...

              Software Zen: delete this;

              H Offline
              H Offline
              honey the codewitch
              wrote on last edited by
              #32

              I feel like if I were in charge of development practices at a given shop and regex were used as documentation as i suggested strict limits would be placed on its use For starters [^-]().+*?| is all you get. That keeps it simple, portable, and non-backtracking. You can easily generate flow diagrams from it. And it keeps people from getting ... "creative"

              Real programmers use butterflies

              G 1 Reply Last reply
              0
              • R realJSOP

                I’ve resisted being hooked on its expedience since it’s inception. The way I see it, I will be able to easily make it to retirement (three years) without getting hooked on it.

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #33

                I don't know that I'll live long enough to retire from coding, though i may retire from doing it professionally. I've always loved it. I suspect I always will. :)

                Real programmers use butterflies

                1 Reply Last reply
                0
                • H honey the codewitch

                  I feel like if I were in charge of development practices at a given shop and regex were used as documentation as i suggested strict limits would be placed on its use For starters [^-]().+*?| is all you get. That keeps it simple, portable, and non-backtracking. You can easily generate flow diagrams from it. And it keeps people from getting ... "creative"

                  Real programmers use butterflies

                  G Offline
                  G Offline
                  Gary R Wheeler
                  wrote on last edited by
                  #34

                  honey the codewitch wrote:

                  it keeps people from getting ... "creative"

                  KISS: Keep It Stupid, Stupid. :cool:

                  Software Zen: delete this;

                  1 Reply Last reply
                  0
                  • S Slacker007

                    we use regex expressions in production code all the time. almost always validation. phone numbers, emails, web addresses, and other pattern specialized validation. never had any issues. performance has never been an issue and accuracy has never been an issue. most of the uses require zero maintenance after implementation.

                    R Offline
                    R Offline
                    Ralf Peter Lucke
                    wrote on last edited by
                    #35

                    I like it. Compared to indexOf, substr etc. it prevents me from dealing with IndexOutOfBound exceptions. I use it often for validation or parsing. I also use it for configuration: Providing a single text field containing a regex, e.g. for selecting files to process, it gives a lot of flexibility at runtime (yes, it requires some knowledge by the administrator, but it can solve a lot of (unforeseen) requirements without changing the application). However, you shouldn't solve *every* problem using regex.

                    1 Reply Last reply
                    0
                    • H honey the codewitch

                      (I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.

                      Real programmers use butterflies

                      Sander RosselS Offline
                      Sander RosselS Offline
                      Sander Rossel
                      wrote on last edited by
                      #36

                      "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." - Jamie Zawinski

                      Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                      H 1 Reply Last reply
                      0
                      • G Gary R Wheeler

                        Mike Hankey wrote:

                        dark matter

                        Dark Matter[^]; great series. A shame it only went three seasons.

                        Software Zen: delete this;

                        M Offline
                        M Offline
                        Matt Bond
                        wrote on last edited by
                        #37

                        It was great right up until that bit where they time-skipped around the future. I could see the downfall of the series coming a mile away. My current theory is that time-travel, if not baked in from the beginning, is a sign the writers have run out of ideas. On the other hand, my daughter hates time travel in movies and shows because it's almost always done wrong. The episode where they went back in time was well done, according to her. Bond Keep all things as simple as possible, but no simpler. -said someone, somewhere

                        1 Reply Last reply
                        0
                        • Sander RosselS Sander Rossel

                          "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." - Jamie Zawinski

                          Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                          H Offline
                          H Offline
                          honey the codewitch
                          wrote on last edited by
                          #38

                          They're not that bad, Sander. Where's your sense of adventure? :-D

                          Real programmers use butterflies

                          1 Reply Last reply
                          0
                          • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                            Me think that most of us do not like it, because of bad experience of implementations in the past by different environments... Almost all of my validations are done in regex, but never done too much of code generations to really use it there...

                            "The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012

                            G Offline
                            G Offline
                            Greg Cronin
                            wrote on last edited by
                            #39

                            I very much enjoy Regex. The complex elegance in a single line of code.

                            1 Reply Last reply
                            0
                            • H honey the codewitch

                              (I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.

                              Real programmers use butterflies

                              T Offline
                              T Offline
                              Thomas Stockwell
                              wrote on last edited by
                              #40

                              It can be quite intimidating at first. I found that Regex 101[^] is a great tool for learning to use regex, and I still use it to validate my regex for projects that I am working on.

                              Regards, Thomas Stockwell

                              1 Reply Last reply
                              0
                              • G Gary R Wheeler

                                Mike Hankey wrote:

                                dark matter

                                Dark Matter[^]; great series. A shame it only went three seasons.

                                Software Zen: delete this;

                                H Offline
                                H Offline
                                Hooga Booga
                                wrote on last edited by
                                #41

                                I enjoyed the series right up until they were able to insert a little card into their existing engine that allowed them to travel anywhere immediately. I know that FTL is imaginary, but I'd think that "blink" might have required a completely different set of physics requiring a new engine or something. My "willing suspension of disbelief" became unwilling at that point.

                                Outside of a dog, a book is a man's best friend; inside of a dog, it's too dark to read. -- Groucho Marx

                                1 Reply Last reply
                                0
                                • H honey the codewitch

                                  (I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.

                                  Real programmers use butterflies

                                  M Offline
                                  M Offline
                                  Matt McGuire
                                  wrote on last edited by
                                  #42

                                  not afraid, but when you only need to make some regex once a year or less, It generally takes a bit to remember the all rules correctly to get the desired results. long regex's can be very cryptic, so I generally tend to break it down with lots of notes about each section for the future me that might have to modify it.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups