M
Marc Scheuner [MVP ADSI]
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)inova.ch
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)inova.ch