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. I love regular expressions

I love regular expressions

Scheduled Pinned Locked Moved The Lounge
designcomgraphicsiot
83 Posts 36 Posters 2 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

    At least the non-backtracking subset. DFA regular expressions. - they are a compact way to describe a simple syntax - they are plain text and brief, easily communicatable and transferable - they are cross platform (at least DFA), running in most any engine - they are incredibly efficient (again, DFA) - they are versatile, able to do validation, tokenization, and matching as well That's probably why they will always be with us. They are maybe the perfect canonical execution of a Chomsky type 3 language. Sure, they can be really terse, but this is as much a strength as it is a weakness, because it facilitates some of the above. I know some people hate them, and I can understand that. But show me a better way.

    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

    J Offline
    J Offline
    Jef fJacobson
    wrote on last edited by
    #46

    I use regular expressions regularly. The downside is that it is difficult for newcomers to understand them. But as long as you add comments to your code it shouldn't be a problem. And many languages allow you to add comments WITHIN the regular expressions, making them even easier to understand. (I frequently create my regexes with comments in C# and then take out the comments afterwards, leaving the original version within commented-out code.)

    1 Reply Last reply
    0
    • F fatman45

      As an IoT person, you probably have done a lot of assembly language programming. I find that people who love regex tend to also like assembly (including myself). I find regex very elegant, and immensely powerful.

      Da Bomb

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

      I actually avoid assembly wherever I can. We have so many different MCUs that come along, and supply is questionable sometimes, which means switching chips mid project. I mean, Arm Cortex M to Arm Cortex M it's not the worst thing in the world, but you'd still have all the CMSIS stuff you'd have to deal with. Ergo, I abstract, and use C++ with ZephyrRTOS for my projects.

      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

      1 Reply Last reply
      0
      • J jmaida

        :) close cobol brings back a lot programming memories

        "A little time, a little trouble, your better day" Badfinger

        B Offline
        B Offline
        bryanren
        wrote on last edited by
        #48

        "cobol".ToUpper()

        1 Reply Last reply
        0
        • J jschell

          honey the codewitch wrote:

          I know some people hate them,

          I certainly don't hate them since I use them quite a bit. Of course no one provides a comment for what they are intended to match. Nor how the match works. But I have seen quite a few that I knew to be wrong. That is concerning.

          honey the codewitch wrote:

          - they are cross platform (at least DFA), running in most any engine

          Not sure what you mean by that. The engine that runs the regex is the only 'cross platform' part possible in that. The regex expressions themselves depend only on the engine so the expression itself has nothing to do with the platform. Perhaps you were referring to that the simplest syntax works in different engines though. But once one uses something more complex that is not necessarily true. But at least with javascript, java and C# they to a great extent use the same syntax.

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

          jschell wrote:

          Not sure what you mean by that.

          What I mean is that regardless of the platform you choose, there is a way to run a DFA regular expression on it. And yeah, that encompasses many different engines, which themselves are what run on a particular platform, unless you're doing code generation, which I do sometimes for them so I don't have to include the regex engine in my firmware. That code is easy to make cross platform. You'd almost have to put in extra effort to make it otherwise. :) I was maybe trying to be too brief by half. I assumed the meaning would come through, but I guess not.

          jschell wrote:

          Perhaps you were referring to that the simplest syntax works in different engines though

          In part yes, but also, virtually every platform has a DFA regex engine for it, or alternatively you can generate DFA code for that platform, with something such as my rxcg project. I was intending to imply that as well.

          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

          J 1 Reply Last reply
          0
          • J jschell

            I have worked at several companies where I was the only one that could do bit fiddling/manipulation. I have worked with developers that did not even know what that was. But I do regexes also.

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

            I have a love hate relationship with bit twiddling. I don't mind writing that code, but I hate having to read it. And I hate the process of commenting it because it tends to take a compact thing and make it necessarily verbose.

            Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

            1 Reply Last reply
            0
            • K k5054

              honey the codewitch wrote:

              A long time ago I wrote a tool.

              Not so long ago I wrote a tool ... Now I'm unemployed :(

              "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

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

              That's why you keep your tool to yourself. :laugh:

              Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

              K 1 Reply Last reply
              0
              • H honey the codewitch

                At least the non-backtracking subset. DFA regular expressions. - they are a compact way to describe a simple syntax - they are plain text and brief, easily communicatable and transferable - they are cross platform (at least DFA), running in most any engine - they are incredibly efficient (again, DFA) - they are versatile, able to do validation, tokenization, and matching as well That's probably why they will always be with us. They are maybe the perfect canonical execution of a Chomsky type 3 language. Sure, they can be really terse, but this is as much a strength as it is a weakness, because it facilitates some of the above. I know some people hate them, and I can understand that. But show me a better way.

                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                J Offline
                J Offline
                Jeroen_R
                wrote on last edited by
                #52

                Being a compact way to describe something is very much the problem. That is exactly why people hate them. You can't just read one and know what it does, nor expect that of your colleagues. Every time I add a RegEx to the code, I have to add an explanation of what it does. Just like in code: if it needs comments to be readable, something's very wrong.

                H 1 Reply Last reply
                0
                • J jschell

                  Mircea Neacsu wrote:

                  why are we so enslaved to tradition that we keep using a layout optimized for the Remington No 2 mechanical typewriter over 100 years ago

                  I believe because, despite claims (and faked data), no alternative has been shown to actually be better. If I recall correct one of the magazines, either "Skeptical Inquirer" or "Skeptic" had at least one article in say the past 10 years that documented the history of that. Possible that there might be some that are as good as, but then that by itself is no reason to switch.

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

                  I recall this too. Qwerty I heard was in part laid out to slow typists down so the mechanical typewriter could keep up. I heard it from the Beagle Bros back in the 1980s so I don't know how true it is. Either way, presumably eventually that wasn't an issue anymore. And Devorak was a common alternative, or at least common as qwerty alternatives go. It was touted as better, but despite the hype I remember reading that it didn't actually improve people's WPM.

                  Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                  J 1 Reply Last reply
                  0
                  • J Jeroen_R

                    Being a compact way to describe something is very much the problem. That is exactly why people hate them. You can't just read one and know what it does, nor expect that of your colleagues. Every time I add a RegEx to the code, I have to add an explanation of what it does. Just like in code: if it needs comments to be readable, something's very wrong.

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

                    Jeroen_R wrote:

                    if it needs comments to be readable, something's very wrong.

                    That's true in the general case, but the exceptions are legion. I'd argue regex is one of those exceptions. There is simply not a more compact and efficient way to tokenize or match flat text than DFA regular expressions. Sure you could try writing it out, but I have. Try your hand at creating a more verbose language than regex and see what it looks like. I wrote one that allows you to describe regular expressions using a BNF notation variant. The result required too much typing, let's put it that way, even though BNF is fairly compact as well. If there's a better mousetrap, nobody seems to have found it yet.

                    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                    1 Reply Last reply
                    0
                    • H honey the codewitch

                      That's why you keep your tool to yourself. :laugh:

                      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                      K Offline
                      K Offline
                      k5054
                      wrote on last edited by
                      #55

                      Not keeping your tool to yourself is one of the leading causes of dismissal, too.

                      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                      J 1 Reply Last reply
                      0
                      • H honey the codewitch

                        At least the non-backtracking subset. DFA regular expressions. - they are a compact way to describe a simple syntax - they are plain text and brief, easily communicatable and transferable - they are cross platform (at least DFA), running in most any engine - they are incredibly efficient (again, DFA) - they are versatile, able to do validation, tokenization, and matching as well That's probably why they will always be with us. They are maybe the perfect canonical execution of a Chomsky type 3 language. Sure, they can be really terse, but this is as much a strength as it is a weakness, because it facilitates some of the above. I know some people hate them, and I can understand that. But show me a better way.

                        Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                        R Offline
                        R Offline
                        Ron Anders
                        wrote on last edited by
                        #56

                        This is the most runaway thread on CP in a long while. Way to poke the bear.

                        1 Reply Last reply
                        0
                        • H honey the codewitch

                          At least the non-backtracking subset. DFA regular expressions. - they are a compact way to describe a simple syntax - they are plain text and brief, easily communicatable and transferable - they are cross platform (at least DFA), running in most any engine - they are incredibly efficient (again, DFA) - they are versatile, able to do validation, tokenization, and matching as well That's probably why they will always be with us. They are maybe the perfect canonical execution of a Chomsky type 3 language. Sure, they can be really terse, but this is as much a strength as it is a weakness, because it facilitates some of the above. I know some people hate them, and I can understand that. But show me a better way.

                          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #57

                          I blank out on regular expressions. Too much like learning a new language. The other day, I needed to get 0 or more leading characters from a string (as an int). I Googled (regex), I went, I left. Coding challenge: get leading digits (I settled for LINQ)

                          var text = "123rd NY 2nd Battalion".
                          //
                          var count = text.TakeWhile( c => Char.IsDigit( c ) ).Count();
                          int i = count > 0 ? ConvertToInt32( text.SubString( 0, count) ) : 0;

                          Answer: 123

                          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                          H B 2 Replies Last reply
                          0
                          • H honey the codewitch

                            It's because I slept. :) But yeah, I get bit overwhelmed when I get a lot of responses, so I kind of respond as I'm able.

                            Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                            M Offline
                            M Offline
                            Mark Starr
                            wrote on last edited by
                            #58

                            I didn’t mean to imply you didn’t participate in the conversation, rather that the topic usually starts up a lively discussion. :cool:

                            Time is the differentiation of eternity devised by man to measure the passage of human events. - Manly P. Hall Mark Just another cog in the wheel

                            1 Reply Last reply
                            0
                            • L Lost User

                              I blank out on regular expressions. Too much like learning a new language. The other day, I needed to get 0 or more leading characters from a string (as an int). I Googled (regex), I went, I left. Coding challenge: get leading digits (I settled for LINQ)

                              var text = "123rd NY 2nd Battalion".
                              //
                              var count = text.TakeWhile( c => Char.IsDigit( c ) ).Count();
                              int i = count > 0 ? ConvertToInt32( text.SubString( 0, count) ) : 0;

                              Answer: 123

                              "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

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

                              ^[0-9]+

                              Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                              1 Reply Last reply
                              0
                              • H honey the codewitch

                                At least the non-backtracking subset. DFA regular expressions. - they are a compact way to describe a simple syntax - they are plain text and brief, easily communicatable and transferable - they are cross platform (at least DFA), running in most any engine - they are incredibly efficient (again, DFA) - they are versatile, able to do validation, tokenization, and matching as well That's probably why they will always be with us. They are maybe the perfect canonical execution of a Chomsky type 3 language. Sure, they can be really terse, but this is as much a strength as it is a weakness, because it facilitates some of the above. I know some people hate them, and I can understand that. But show me a better way.

                                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                                B Offline
                                B Offline
                                BernardIE5317
                                wrote on last edited by
                                #60

                                i enjoy the challenge of learning / writing regular expressions for my text editing of source code . as in all things mastering it is the same as being invited to perform at Carnegie Hall id est "practice practice practice" . my purpose in this post though is to express my impression from these many and expert posts utilizing fancy schmancy terms of which i do not know which seem to indicate regular expressions are more powerful / advanced / sophisticated than i know as i understand them to be nothing more than a text editing convenience . may i please inquire am i wrong in this regard . thank you kindly .

                                H J 2 Replies Last reply
                                0
                                • B BernardIE5317

                                  i enjoy the challenge of learning / writing regular expressions for my text editing of source code . as in all things mastering it is the same as being invited to perform at Carnegie Hall id est "practice practice practice" . my purpose in this post though is to express my impression from these many and expert posts utilizing fancy schmancy terms of which i do not know which seem to indicate regular expressions are more powerful / advanced / sophisticated than i know as i understand them to be nothing more than a text editing convenience . may i please inquire am i wrong in this regard . thank you kindly .

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

                                  They're for text processing, but for more than text editing. The C# compiler for example, almost certainly uses a tokenizer built up of regular expressions. That said, you're basically not wrong. I mean, tokenization is almost like text matching, but with a twist.

                                  Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                                  1 Reply Last reply
                                  0
                                  • G giulicard

                                    Joking aside, more trivially I believe the term "regular" refers to the third level of Chomsky's hierarchy, which, precisely, is defined as Type3-Regular. DFA (Deterministic Finite Automaton) are FSA (Finite State Automaton).

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

                                    Can confirm.

                                    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                                    1 Reply Last reply
                                    0
                                    • H honey the codewitch

                                      A modest proposal: Learn the DFA subset. Commit it to memory, and forget the rest. DFA is the non-backtracking subset of regular expressions () - capture and group [] - match char ranges * - match zero or more + - match one or more ? - match zero or one . - match any single character | - match a or b (a|b)

                                      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                                      J Offline
                                      J Offline
                                      jmaida
                                      wrote on last edited by
                                      #63

                                      Agree. A good and practical recommendation!

                                      "A little time, a little trouble, your better day" Badfinger

                                      1 Reply Last reply
                                      0
                                      • G GuyThiebaut

                                        I used ChatGPT precisely for that and it returned a decent regex with an explanation. I needed to word my question in a manner that was generic but the result was actually helpful.

                                        “That which can be asserted without evidence, can be dismissed without evidence.”

                                        ― Christopher Hitchens

                                        J Offline
                                        J Offline
                                        jmaida
                                        wrote on last edited by
                                        #64

                                        cool. forgot chatgpt is hanging out there

                                        "A little time, a little trouble, your better day" Badfinger

                                        G 1 Reply Last reply
                                        0
                                        • K k5054

                                          Not keeping your tool to yourself is one of the leading causes of dismissal, too.

                                          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                                          J Offline
                                          J Offline
                                          jmaida
                                          wrote on last edited by
                                          #65

                                          lordy... "its a bad worker who blames their tools." "there is no tool like an old tool." :)

                                          "A little time, a little trouble, your better day" Badfinger

                                          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