in reply to Re: Extraction number from Text
in thread Extraction number from Text
I allowed more than one \d digit, with \d+.
It took me a while to grok it, but the original regex did allow more than one digit, and also captures it. That's because there is a \d in the character class, and the character class is quantified with a *. However this also allows more than one dot or more than one e, so it recognizes Ee.3 as a number.
I agree that your regex is much better to read, but it doesn't allowe numbers before the exponential (I guess that's what the e in the regex is supposed to mean).
Further refinements could use Regex::Common's number regex, or this regex, which parses numbers according to the JSON number specification:
my $number = qr{ -? (?: 0 | [1-9] [0-9]* ) (?: \. [0-9]+ )? (?: [eE] [+-]? [0-9] )? }x;
(might be a bit too restrictive in some cases for parsing numbers "in the wild", but still a good inspiration).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Extraction number from Text
by davido (Cardinal) on Jun 14, 2010 at 08:50 UTC | |
by JavaFan (Canon) on Jun 14, 2010 at 10:08 UTC | |
by proceng (Scribe) on Jun 14, 2010 at 13:51 UTC |