Greetings, i have been looking for a better way to write a regex for determining if a string is a number, including integers, floating pt numbers, and scientific notation.
seems so obfuscated that it has to be wrong. please point out errors, omissions, etc.
so far, i have looked at cpan and the perlmonks archive. i have only found regexs for more well-behaved strings.
the output i need to parse includes (some of these are ill-formed, but "it's a liven'!":
1.0
-1.0
.9
9.
-.9
-.4e08
+4.E-08
+.2e+219
2.00001e+9
the code that i have and not sure about is:
our $numregex = qr/^
# may have leading plus or minus
[-+]?
# one of these formats must exist
(?:
# maybe one or more digits, then decimal, and maybe more dig
+its
\d+\.?\d*
# or decimal followed by one or more digits
|\.\d+
)
# optional sci notation
(?:
# "e" or "E" and plus or minus, and one, two, or three digit
+s
[eE][-+]\d{1,3}
)?
$/x;
thank you.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.