Reputation: 13
What's a good regex to match a decimal number to check that it does not contain exponential values?
Thanks for any help.
Can I just say something like match anything except if it contains "e-", "e+", "E-" or "E+"?
Upvotes: 1
Views: 1879
Reputation: 10337
The thing is its a cost field, and can contain currency symbols, parenthesis and other characters like that
Without detailed specs it's not an easy task using regular expressions. In my opinion, regex is inappropriate when you know so little about the format of your input.
Update after OP's edit:
Can I just say something like match anything except if it contains "e-", "e+", "E-" or "E+"?
That would be e.g. ^(?!.*[eE][+-]).*$
(using a negative lookahead), but probably matching more than you like…
Upvotes: 1
Reputation: 15294
While it's not entirely clear to me what you're looking for, you might want to take a look at the Regexp::Common
module, which is available from CPAN, specifically at Regexp::Common::number
. It might offer what you're after.
Upvotes: 0
Reputation: 63952
This is not the shortest solution, but check the correctness of the whole number...
if( $num =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ ) {
#correct floating point
if( $num =~ /e/i ) {
#exponential
} else {
#not exponential
}
}
Upvotes: 2