Rookie thoughts on Regex--useful but not complete

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raylopez99

    Rookie thoughts on Regex--useful but not complete

    I went through a bunch of Regex examples, and indeed it's quite
    powerful, including 'groups' using 'matches', word boundaries,
    lookahead matches, replacing and splitting text,etc--apparently
    there's a whole book on it, though I just used the chapter in
    Albahari et al C#3 in a Nutshell (a great book). You can indeed
    'massage' a string into a number of ways, but, at the end of the
    process I found that you still have to go through the 'massaged'
    string, character by character, and do "fine tuning". I'm sure you
    can also do a Regex to do such "fine tuning" but I am at a dead end
    for a number of problems and nothing beats a character by character
    analysis.

    For example, suppose you want to extract all the numbered coefficents
    10a, 11d, 12g and 100f from the following: given the string
    "xyz10abc11def1 2gh100f".

    string sMY = "xyz10abc11def1 2gh100f";

    Match mMY = Regex.Match(sMY , @"\d\d\D");
    Console.WriteLi ne("result mMY-->{0}", mMY.Value);
    Regex r001 = new Regex(@"\d\d\D" );
    MatchCollection mc2 = r001.Matches(sM Y);

    foreach (Match m in mc2)
    {
    Console.WriteLi ne("m: {0}", m.Value);
    }

    You'll get output (as an array of strings): 10a, 11d, 12g, 00f, which
    is not quite what you want, because of the last value is "00f" not
    "100f". So you still have to go through the string "manually" and
    extract these "end cases" or "corner cases".

    Now you Regex experts I'm sure can find the exact Regex to give the
    proper answer (it would help if there's a 'conditional' Regex
    expression so you can do either two digits or three digits, but I'm
    not aware of one), however, in the general case, I think I'm correct
    in saying that while Regex helps, it cannot get all the corner cases.

    RL
  • Chris Dunaway

    #2
    Re: Rookie thoughts on Regex--useful but not complete

    On Nov 17, 8:44 am, raylopez99 <raylope...@yah oo.comwrote:
    You'll get output (as an array of strings): 10a, 11d, 12g, 00f, which
    is not quite what you want, because of the last value is "00f" not
    "100f". So you still have to go through the string "manually" and
    extract these "end cases" or "corner cases".
    >
    Now you Regex experts I'm sure can find the exact Regex to give the
    proper answer (it would help if there's a 'conditional' Regex
    expression so you can do either two digits or three digits, but I'm
    not aware of one), however, in the general case, I think I'm correct
    in saying that while Regex helps, it cannot get all the corner cases.
    You can use this expression:

    @"\d{2,3}\D"

    which returns 10a, 11d, 12g, and 100f.

    The {2,3} applies to the digit (\d) and tells it to look for at least
    2 digits but no more than three. You can use the pipe character to
    indicate an "or" condition in a regex. For example if your string was
    "balltallcallfa llhallmall" and you used this regex: "[t|h]all" it
    would return matches "tall" and "hall"

    Here is a site with many regular expressions. You can examine them
    and see how they work. Perhaps it will help in your study.



    Chris

    Comment

    • Tom Dacon

      #3
      Re: Rookie thoughts on Regex--useful but not complete


      "raylopez99 " <raylopez99@yah oo.comwrote in message
      news:5d17bd53-1500-4373-9b52-53dc8cdb87d8@v5 g2000prm.google groups.com...
      Now you Regex experts I'm sure can find the exact Regex to give the
      proper answer
      however, in the general case, I think I'm correct
      in saying that while Regex helps, it cannot get all the corner cases.
      Your first point contradicts your second.

      The Regex syntax is ugly and demanding, there's no question about that. It's
      difficult, if not impossible, to think of another programming syntax that
      comes close for ugliness and lack of readability. My intuition tells me that
      there are few programmers who consider themselves competent in it, and far
      far fewer that can make the claim that they've mastered it.

      Nevertheless, it's as powerful as your understanding of it. Rather than
      disparage it, I'd suggest that you continue your study by searching for
      examples on the web (perhaps consider buying the book that you mentioned!)
      and develop for yourself the ability to solve problems such as the simple
      one you posed (another poster has already given you the solution).

      Tom Dacon
      Dacon Software Consulting



      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: Rookie thoughts on Regex--useful but not complete

        On Nov 17, 11:36 am, "Tom Dacon" <tda...@communi ty.nospamwrote:
        "raylopez99 " <raylope...@yah oo.comwrote in message
        >
        news:5d17bd53-1500-4373-9b52-53dc8cdb87d8@v5 g2000prm.google groups.com...
        >
        Now you Regex experts I'm sure can find the exact Regex to give the
        proper answer
        >
         however, in the general case, I think I'm correct
        >
        in saying that while Regex helps, it cannot get all the corner cases.
        >
        Your first point contradicts your second.
        >
        The Regex syntax is ugly and demanding, there's no question about that. It's
        difficult, if not impossible, to think of another programming syntax that
        comes close for ugliness and lack of readability. My intuition tells me that
        there are few programmers who consider themselves competent in it, and far
        far fewer that can make the claim that they've mastered it.
        >
        Nevertheless, it's as powerful as your understanding of it. Rather than
        disparage it, I'd suggest that you continue your study by searching for
        examples on the web (perhaps consider buying the book that you mentioned!)
        and develop for yourself the ability to solve problems such as the simple
        one you posed (another poster has already given you the solution).
        >
        Tom Dacon
        Dacon Software Consulting
        How old are you?
        Most probably the Regex have been aroudn since before you were born :)
        They are not easy to understand at first but later you can deconstruct
        most of then easily.

        Comment

        • Jeff Johnson

          #5
          Re: Rookie thoughts on Regex--useful but not complete

          "Chris Dunaway" <dunawayc@gmail .comwrote in message
          news:70b681b2-f374-4a1e-8f61-0e0d3a3d1147@o4 0g2000prn.googl egroups.com...
          Also: http://www.regular-expressions.info


          Comment

          • Tom Dacon

            #6
            Re: Rookie thoughts on Regex--useful but not complete


            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
            message
            news:b7a214b9-f1d9-49e7-9b41-daf48a079493@o4 0g2000prn.googl egroups.com...
            On Nov 17, 11:36 am, "Tom Dacon" <tda...@communi ty.nospamwrote:
            "raylopez99 " <raylope...@yah oo.comwrote in message
            >
            news:5d17bd53-1500-4373-9b52-53dc8cdb87d8@v5 g2000prm.google groups.com...
            >
            >How old are you?
            >Most probably the Regex have been aroudn since before you were born :)
            >They are not easy to understand at first but later you can deconstruct
            >most of then easily.

            I take it that your question is to me, not to the OP.

            I'm 65, and I've been continuously in the software industry since I was 19 -
            that would be 1962. I'm currently a .Net software architect, with my .Net
            experience going back to about a month after the NDA was lifted at the PDC
            conference in LA in 2000. I program these days in C# and VB. Previous
            experience was in Unix, various microcomputers, and IBM mainframes. I
            started out on PC's in 1982 on DOS 1.0.

            My first introduction to regular expressions occurred in the mid to late
            1980's, IIRC, when I was given the task of integrating regular expressions
            into some proprietary text search software at the company I was then working
            for. That was in the C language, I believe, although parts of it were done
            in assembly language. It was also around then that I started writing
            documentation in HTML's predecessor, SGML, but that's another story.

            I stand by my opinion of regular expression syntax. Doing work with regular
            expressions is an infrequent task for most programmers. Anything you do
            quite infrequently poses enormous loads on your memory. Because the syntax
            is visually noisy and unstructured, because it cannot be easily organized to
            let our own visual pattern-matching capabilities help in extracting the
            sense out of an expression, I personally think that it's a horror, both to
            write and to read. This in spite of the fact that I'm as competent at it
            myself as I occasionally need to be. You can do something well, but still
            not like it.

            Your opinion may differ, and I cannot deny you your own. This is mine.

            BTW, in 1962 how old were the people who would later become your parents? No
            offense intended, of course :-)

            Tom Dacon
            Dacon Software Consulting





            Comment

            • Johnson

              #7
              Re: Rookie thoughts on Regex--useful but not complete

              I take it that your question is to me, not to the OP.

              It appeared to me - an independent observer here - that it was intended for
              the OP. The OP here has a long history of posting his observations, musings,
              opinions, and rants - most of which serve no purpose other than to showcase
              his lack of knowledge and comprehension of .NET development (or software
              development in general) coupled with his need to share his glib thoughts
              (just like how he kicked off the current thread).





              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: Rookie thoughts on Regex--useful but not complete

                On Nov 17, 3:36 pm, "Tom Dacon" <tda...@communi ty.nospamwrote:
                "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in
                messagenews:b7a 214b9-f1d9-49e7-9b41-daf48a079493@o4 0g2000prn.googl egroups..com...
                On Nov 17, 11:36 am, "Tom Dacon" <tda...@communi ty.nospamwrote:
                >
                "raylopez99 " <raylope...@yah oo.comwrote in message
                >
                news:5d17bd53-1500-4373-9b52-53dc8cdb87d8@v5 g2000prm.google groups.com...
                >
                How old are you?
                Most probably the Regex have been aroudn since before you were born :)
                They are not easy to understand at first but later you can deconstruct
                most of then easily.
                >
                I take it that your question is to me, not to the OP.
                It was for the OP
                When I posted it (at least when I read it) your post was not there
                yet :)

                Comment

                • Jeff Johnson

                  #9
                  Re: Rookie thoughts on Regex--useful but not complete

                  "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                  message
                  news:819d0782-19f7-4eff-9cdd-34d37ecc225e@40 g2000prx.google groups.com...
                  I take it that your question is to me, not to the OP.
                  It was for the OP
                  When I posted it (at least when I read it) your post was not there
                  yet :)
                  ....then how did you manage to quote his post?!


                  Comment

                  • Peter Duniho

                    #10
                    Re: Rookie thoughts on Regex--useful but not complete

                    On Mon, 17 Nov 2008 13:28:15 -0800, Jeff Johnson <i.get@enough.s pamwrote:
                    "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                    message
                    news:819d0782-19f7-4eff-9cdd-34d37ecc225e@40 g2000prx.google groups.com...
                    >
                    >I take it that your question is to me, not to the OP.
                    >
                    >It was for the OP
                    >When I posted it (at least when I read it) your post was not there
                    >yet :)
                    >
                    ...then how did you manage to quote his post?!
                    Actually, he's using Google Groups. It's a terrible way to post, and it's
                    clear to me from the various posting errors that occur from people using
                    it that it's got a terrible, misleading and buggy user-interface.

                    I'd guess that Ignacio really did think he was replying to the original
                    post, but when he finally hit the "Post" button (or "Submit" or whatever
                    Google calls it), Google "helpfully" added the quote from the
                    newly-arrived post from Tom and made Ignacio's post refer to that one
                    instead of the original post as Ignacio expected, creating the confusion
                    we've got here.

                    One clue in favor of this theory is that Ignacio has the good habit of
                    trimming quotes, but in that post, the quote wasn't trimmed at all,
                    suggesting that he never had a chance to notice the quote was being
                    included at all.

                    Just one more reason for people to avoid like the plague using Google
                    Groups as a newsreader. It's fine if you want to do a search (assuming it
                    hasn't lost the post you're looking for), but as a portal for someone to
                    actually _contribute_, it's woefully flawed.

                    Pete

                    Comment

                    • Tom Dacon

                      #11
                      Re: Rookie thoughts on Regex--useful but not complete

                      Even using another newsreader, I've myself occasionally replied to the wrong
                      post. Probably just about everyone who's active on the ng's has done it one
                      time or another. If it was a serious trangression of netiquette, we'd
                      probably all be hanging from the branch of a tree by our necks, swaying
                      slowly in the wind :-)

                      Tom

                      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                      news:op.ukr9iko a8jd0ej@petes-computer.local. ..
                      On Mon, 17 Nov 2008 13:28:15 -0800, Jeff Johnson <i.get@enough.s pam>
                      wrote:
                      >
                      >"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                      >message
                      >news:819d078 2-19f7-4eff-9cdd-34d37ecc225e@40 g2000prx.google groups.com...
                      >>
                      >>I take it that your question is to me, not to the OP.
                      >>
                      >>It was for the OP
                      >>When I posted it (at least when I read it) your post was not there
                      >>yet :)
                      >>
                      >...then how did you manage to quote his post?!
                      >
                      Actually, he's using Google Groups. It's a terrible way to post, and it's
                      clear to me from the various posting errors that occur from people using
                      it that it's got a terrible, misleading and buggy user-interface.
                      >
                      I'd guess that Ignacio really did think he was replying to the original
                      post, but when he finally hit the "Post" button (or "Submit" or whatever
                      Google calls it), Google "helpfully" added the quote from the
                      newly-arrived post from Tom and made Ignacio's post refer to that one
                      instead of the original post as Ignacio expected, creating the confusion
                      we've got here.
                      >
                      One clue in favor of this theory is that Ignacio has the good habit of
                      trimming quotes, but in that post, the quote wasn't trimmed at all,
                      suggesting that he never had a chance to notice the quote was being
                      included at all.
                      >
                      Just one more reason for people to avoid like the plague using Google
                      Groups as a newsreader. It's fine if you want to do a search (assuming it
                      hasn't lost the post you're looking for), but as a portal for someone to
                      actually _contribute_, it's woefully flawed.
                      >
                      Pete

                      Comment

                      • Peter Duniho

                        #12
                        Re: Rookie thoughts on Regex--useful but not complete

                        On Mon, 17 Nov 2008 13:58:48 -0800, Tom Dacon <tdacon@communi ty.nospam>
                        wrote:
                        Even using another newsreader, I've myself occasionally replied to the
                        wrong
                        post.
                        Even quoted it? Well, actually...sinc e you top-post, maybe you have. :p
                        But it doesn't seem like the mistake Ignacio would have made with a news
                        posting tool that actually showed him what he was about to do. So I blame
                        Google.
                        Probably just about everyone who's active on the ng's has done it one
                        time or another. [...]
                        I agree that I wouldn't fault someone for making an honest mistake like
                        that, but I can't say that I've ever done it myself. I suspect there's a
                        lot more people who haven't than you suspect. :)

                        Pete

                        Comment

                        • Jeff Johnson

                          #13
                          Re: Rookie thoughts on Regex--useful but not complete

                          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
                          news:op.ukr9iko a8jd0ej@petes-computer.local. ..
                          Just one more reason for people to avoid like the plague using Google
                          Groups as a newsreader. It's fine if you want to do a search (assuming it
                          hasn't lost the post you're looking for), but as a portal for someone to
                          actually _contribute_, it's woefully flawed.
                          A-freakin'-men. I don't care if you use Agent, Thunderbird, or even (gasp!)
                          Outlook Express*, just use a newsreader people!



                          *Or any other newsreader out there; those were just the ones I came up with
                          off the wtop of my head.


                          Comment

                          • Ignacio Machin ( .NET/ C# MVP )

                            #14
                            Re: Rookie thoughts on Regex--useful but not complete

                            On Nov 17, 4:28 pm, "Jeff Johnson" <i....@enough.s pamwrote:
                            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in
                            messagenews:819 d0782-19f7-4eff-9cdd-34d37ecc225e@40 g2000prx.google groups.com...
                            >
                            I take it that your question is to me, not to the OP.
                            It was for the OP
                            When I posted it (at least when I read it) your post was not there
                            yet :)
                            >
                            ...then how did you manage to quote his post?!
                            When I posted the FIRST post TOP's was not there. when I posted the
                            second it was and my second post is the answer to it

                            Comment

                            • Ignacio Machin ( .NET/ C# MVP )

                              #15
                              Re: Rookie thoughts on Regex--useful but not complete

                              On Nov 17, 4:48 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                              wrote:
                              On Mon, 17 Nov 2008 13:28:15 -0800, Jeff Johnson <i....@enough.s pamwrote:
                              "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in  
                              message
                              news:819d0782-19f7-4eff-9cdd-34d37ecc225e@40 g2000prx.google groups.com...
                              >
                              I take it that your question is to me, not to the OP.
                              >
                              It was for the OP
                              When I posted it (at least when I read it) your post was not there
                              yet :)
                              >
                              ...then how did you manage to quote his post?!
                              >
                              Actually, he's using Google Groups.  It's a terrible way to post, and it's  
                              clear to me from the various posting errors that occur from people using  
                              it that it's got a terrible, misleading and buggy user-interface.
                              >
                              I'd guess that Ignacio really did think he was replying to the original  
                              post, but when he finally hit the "Post" button (or "Submit" or whatever  
                              Google calls it), Google "helpfully" added the quote from the  
                              newly-arrived post from Tom and made Ignacio's post refer to that one  
                              instead of the original post as Ignacio expected, creating the confusion  
                              we've got here.
                              >
                              One clue in favor of this theory is that Ignacio has the good habit of  
                              trimming quotes, but in that post, the quote wasn't trimmed at all,  
                              suggesting that he never had a chance to notice the quote was being  
                              included at all.
                              >
                              Just one more reason for people to avoid like the plague using Google  
                              Groups as a newsreader.  It's fine if you want to do a search (assumingit  
                              hasn't lost the post you're looking for), but as a portal for someone to  
                              actually _contribute_, it's woefully flawed.
                              >
                              Pete
                              Opps

                              You are right :) I did responded to Tom's post originally.
                              TOM: My appologies it was intended to the OP.

                              Peter:
                              You are right, google groups is not the best newsreader around :(.
                              But it's one of the only I have access from the office.

                              Comment

                              Working...