开发者

Perl regex - exponential values

What's a good regex to match a decimal number to check that it do开发者_StackOverflow社区es 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+"?


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
    }
}


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…


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜