Regular expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Scheuner [MVP ADSI]

    #1

    Regular expressions

    Folks,

    I've started using regular expressions for parsing some data string
    that come along - works quite nicely, however, as a newbie to RE, I'm
    still struggling with some special cases:

    1) I get strings representing decimal values, and they have the
    following format:
    * first, a plus or minus sign
    * then, a number of numeric chars
    * if the precision is > 0, then a dot and "precision" number of
    numerical characters will follow

    This last thing is what's killing me - how can I put the "conditial"
    thing ("if precision > 0, then a dot and digits) into a single regular
    expression?

    What I have now is this:

    [+-]\d{x,x}.\d{p,p}

    which works fine, as long as I have a DECIMAL number with x digits
    before and p digits after the comma (e.g. +120.45 if x=3 and p=2).

    However, if p=0, this string won't be matched +120 since it doesn't
    have the dot at the end.........

    Any ideas??

    2) For a date, which comes along as DD.MM.YYYY, I'd like to be able to
    match both the cases where DD is either "01" or " 1" (leading 0 or
    space). I tried various things, but nothing seems to work - it seems
    if I allow it to have a leading whitespace, it'll also match "
    01.12.2003" (leading whitespace and then two digits for the day) -
    this is *not* what I want!

    I've tried \d{2,2}.\d{2,2} .\d{4,4}, which works fine if the day is
    specified with a leading zero - but will fail if I pass it "
    1.12.2003". I also tried (\s\d|\d{2,2}). \d{2,2}.\d{4,4} , but as I
    mentioned - in this case, all these dates are being matched:
    "01.01.2003 " - okay
    " 1.01.2003" - okay
    " 01.01.2003" - NOT okay (leading zero + 2 digits)

    Any takers?

    Thanks!
    Marc

    =============== =============== =============== =============== ====
    Marc Scheuner May The Source Be With You!
    Bern, Switzerland m.scheuner(at)i nova.ch
  • Marc Scheuner [MVP ADSI]

    #2
    Re: Regular expressions

    Got another riddle:

    how can I make a regular expression that will match all of the
    following:

    DECIMAL
    DECIMAL(5)
    DECIMAL(15, 5) (fifteen-comma-space-five)

    Anyone? I've tried numerous expressions - either I get too much
    (everything), or I get the two last ones (with the parenthesis) - but
    I can't seem to make it work for all three cases in just one
    expression..... ..

    Thanks!
    Marc

    =============== =============== =============== =============== ====
    Marc Scheuner May The Source Be With You!
    Bern, Switzerland m.scheuner(at)i nova.ch

    Comment

    • Brian Davis

      #3
      Regular expressions

      For #1, I don't know of a way to match based on
      arithmetical comparisons (like p>0), so you would probably
      have to use two different expressions:

      if p>0 -> [+-]\d{x}.\d{p}
      if p=0 -> [+-]\d{x}

      This expression will make sure that a result does not end
      with only a decimal point, but it will accept -123 even if
      p>0:

      [+-]\d{x}(?(\.\d).\ d{p})$


      Additionally, if you just wanted to make sure that no more
      than p digits followed the decimal point, you could use
      this:

      [+-]\d{x}(?(\.\d).\ d{0,p})$


      ----------------------------------------
      For #2, The last expression you listed:

      (\s\d|\d{2,2}). \d{2,2}.\d{4,4}

      matches corredtly. If you check the Match object's value,
      it will not include the leading space if a leading zero
      exists. If you want to make sure that the input string
      contains only what you are trying to match, place ^
      (beginning of string) at the beginning and $(end of
      string) at the end:

      ^(\s\d|\d{2}).\ d{2}.\d{4}$


      Hope this helps,

      Brian Davis


      [color=blue]
      >-----Original Message-----
      >Folks,
      >
      >I've started using regular expressions for parsing some[/color]
      data string[color=blue]
      >that come along - works quite nicely, however, as a[/color]
      newbie to RE, I'm[color=blue]
      >still struggling with some special cases:
      >
      >1) I get strings representing decimal values, and they[/color]
      have the[color=blue]
      >following format:
      >* first, a plus or minus sign
      >* then, a number of numeric chars
      >* if the precision is > 0, then a dot and "precision"[/color]
      number of[color=blue]
      >numerical characters will follow
      >
      >This last thing is what's killing me - how can I put[/color]
      the "conditial"[color=blue]
      >thing ("if precision > 0, then a dot and digits) into a[/color]
      single regular[color=blue]
      >expression?
      >
      >What I have now is this:
      >
      >[+-]\d{x,x}.\d{p,p}
      >
      >which works fine, as long as I have a DECIMAL number with[/color]
      x digits[color=blue]
      >before and p digits after the comma (e.g. +120.45 if x=3[/color]
      and p=2).[color=blue]
      >
      >However, if p=0, this string won't be matched +120[/color]
      since it doesn't[color=blue]
      >have the dot at the end.........
      >
      >Any ideas??
      >
      >2) For a date, which comes along as DD.MM.YYYY, I'd like[/color]
      to be able to[color=blue]
      >match both the cases where DD is either "01" or " 1"[/color]
      (leading 0 or[color=blue]
      >space). I tried various things, but nothing seems to[/color]
      work - it seems[color=blue]
      >if I allow it to have a leading whitespace, it'll also[/color]
      match "[color=blue]
      >01.12.2003" (leading whitespace and then two digits for[/color]
      the day) -[color=blue]
      >this is *not* what I want!
      >
      >I've tried \d{2,2}.\d{2,2} .\d{4,4}, which works fine if[/color]
      the day is[color=blue]
      >specified with a leading zero - but will fail if I pass[/color]
      it "[color=blue]
      >1.12.2003". I also tried (\s\d|\d{2,2}). \d{2,2}.\d{4,4} ,[/color]
      but as I[color=blue]
      >mentioned - in this case, all these dates are being[/color]
      matched:[color=blue]
      > "01.01.2003 " - okay
      > " 1.01.2003" - okay
      > " 01.01.2003" - NOT okay (leading zero + 2 digits)
      >
      >Any takers?
      >
      >Thanks!
      >Marc
      >
      >============== =============== =============== ==============[/color]
      ======[color=blue]
      >Marc Scheuner May The Source Be[/color]
      With You![color=blue]
      >Bern, Switzerland m.scheuner(at)[/color]
      inova.ch[color=blue]
      >.
      >[/color]

      Comment

      • Brian Davis

        #4
        Re: Regular expressions


        Try this one:

        ^DECIMAL(\(\d+( , \d+)?\))?$


        Brian Davis

        [color=blue]
        >-----Original Message-----
        >Got another riddle:
        >
        >how can I make a regular expression that will match all[/color]
        of the[color=blue]
        >following:
        >
        >DECIMAL
        >DECIMAL(5)
        >DECIMAL(15, 5) (fifteen-comma-space-five)
        >
        >Anyone? I've tried numerous expressions - either I get[/color]
        too much[color=blue]
        >(everything) , or I get the two last ones (with the[/color]
        parenthesis) - but[color=blue]
        >I can't seem to make it work for all three cases in just[/color]
        one[color=blue]
        >expression.... ...
        >
        >Thanks!
        >Marc
        >
        >============== =============== =============== ==============[/color]
        ======[color=blue]
        >Marc Scheuner May The Source Be[/color]
        With You![color=blue]
        >Bern, Switzerland m.scheuner(at)[/color]
        inova.ch[color=blue]
        >.
        >[/color]

        Comment

        • Eric Gunnerson [MS]

          #5
          Re: Regular expressions

          Marc,

          The usual way to do what you want is to use the '?' quantifier, which means
          "match 0 or 1 time". So, to add the plus or minus, you get:

          (+|-)?\d+

          plus or minus one or zero times followed by one or more digits.

          Add in the decimal part:

          (+|-)?\d+(\.\d+)?

          adds in "." followed by one or more digits, match the whole thing zero or
          one time.


          Lastly, you might want to download my regular expression workbench at the
          csharp site below. It will make playing with regex much easier.

          --
          Eric Gunnerson

          Visit the C# product team at http://www.csharp.net
          Eric's blog is at http://blogs.gotdotnet.com/ericgu/

          This posting is provided "AS IS" with no warranties, and confers no rights.
          "Marc Scheuner [MVP ADSI]" <m.scheuner@ino va.SPAMBEGONE.c h> wrote in message
          news:hdh3kvssm6 vmi91s52tif9ttp 91jcvhe33@4ax.c om...[color=blue]
          > Folks,
          >
          > I've started using regular expressions for parsing some data string
          > that come along - works quite nicely, however, as a newbie to RE, I'm
          > still struggling with some special cases:
          >
          > 1) I get strings representing decimal values, and they have the
          > following format:
          > * first, a plus or minus sign
          > * then, a number of numeric chars
          > * if the precision is > 0, then a dot and "precision" number of
          > numerical characters will follow
          >
          > This last thing is what's killing me - how can I put the "conditial"
          > thing ("if precision > 0, then a dot and digits) into a single regular
          > expression?
          >
          > What I have now is this:
          >
          > [+-]\d{x,x}.\d{p,p}
          >
          > which works fine, as long as I have a DECIMAL number with x digits
          > before and p digits after the comma (e.g. +120.45 if x=3 and p=2).
          >
          > However, if p=0, this string won't be matched +120 since it doesn't
          > have the dot at the end.........
          >
          > Any ideas??
          >
          > 2) For a date, which comes along as DD.MM.YYYY, I'd like to be able to
          > match both the cases where DD is either "01" or " 1" (leading 0 or
          > space). I tried various things, but nothing seems to work - it seems
          > if I allow it to have a leading whitespace, it'll also match "
          > 01.12.2003" (leading whitespace and then two digits for the day) -
          > this is *not* what I want!
          >
          > I've tried \d{2,2}.\d{2,2} .\d{4,4}, which works fine if the day is
          > specified with a leading zero - but will fail if I pass it "
          > 1.12.2003". I also tried (\s\d|\d{2,2}). \d{2,2}.\d{4,4} , but as I
          > mentioned - in this case, all these dates are being matched:
          > "01.01.2003 " - okay
          > " 1.01.2003" - okay
          > " 01.01.2003" - NOT okay (leading zero + 2 digits)
          >
          > Any takers?
          >
          > Thanks!
          > Marc
          >
          > =============== =============== =============== =============== ====
          > Marc Scheuner May The Source Be With You!
          > Bern, Switzerland m.scheuner(at)i nova.ch[/color]


          Comment

          • Marc Scheuner [MVP ADSI]

            #6
            Re: Regular expressions

            >The usual way to do what you want is to use the '?' quantifier, which means[color=blue]
            >"match 0 or 1 time". So, to add the plus or minus, you get:
            >Add in the decimal part:
            >(+|-)?\d+(\.\d+)?[/color]

            Yeah, I stumbled across that after a while of *not* looking at my
            regex :-) Thanks.
            [color=blue]
            >Lastly, you might want to download my regular expression workbench at the
            >csharp site below. It will make playing with regex much easier.[/color]

            Excellent, thanks so much!

            Marc

            =============== =============== =============== =============== ====
            Marc Scheuner May The Source Be With You!
            Bern, Switzerland m.scheuner(at)i nova.ch

            Comment

            • Marc Scheuner [MVP ADSI]

              #7
              Re: Regular expressions

              >Try this one:[color=blue]
              >^DECIMAL(\(\d+ (, \d+)?\))?$[/color]

              Thanks - it would work, trouble is, I also need to recognize other
              types such as DATE, INTEGER, CHAR(x), VARCHAR(y) and so forth, and
              they're not on a line of their own (so I can't use the ^ and $
              delimiters).

              I think I got it figured out by now - thanks for your input! Highly
              appreciated.

              Marc

              =============== =============== =============== =============== ====
              Marc Scheuner May The Source Be With You!
              Bern, Switzerland m.scheuner(at)i nova.ch

              Comment

              Working...