in reply to Matching floats according to perlfaq4

At the risk of once again showing my lack of aptitude for regexes, below is what I have currently settled on. It handles all the cases you list, accepting or rejecting them as appropriate.

This seems like a good opportunity to get another set of eyes to look it over.

my $num = qr! [+-]? (?: \d*? )? (?: (?<=\d)\.? | \.?(?=\d) ) (?: \d*?)? (?: \d[Ee] (?: [+-]?\d+ ) )? !ox;

I'm sure there is something wrong with it, but so far it has passed everything I have throw at it.

It was ponted out to me (thankyou sauoq) that this only works if used in a context where it is tightly anchored on both ends.

Minimally, m/^$num$/o.

Note: \b wont work.

For my current learning exercise, that is fine as it will always be used as a part of a larger regex that will so constrain it.

If anyone wants to point out how to modify it to make it work without anchoring, I like to see and note the changes for my ed.

For 'real work' I would use Regex::common of course, especially given it heritage and guardianship. Now if only there was a Regex::Uncommon that did everything else, I wouldn't have to learn regexes at all:)


Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Replies are listed 'Best First'.
Re: Re: Matching floats according to perlfaq4
by demerphq (Chancellor) on Sep 27, 2002 at 11:41 UTC
    I dont like this regex. It seems to me to be more complicated (and thus less efficent) than the FAQ version with no added benefit. For instance why replace (?=\d|\.\d)\d*(\.\d*)? with all of this
    (?: \d*? )? (?: (?<=\d)\.? | \.?(?=\d) ) (?: \d*?)? (?: \d
    (the last paren is unterminated deliberately)

    --- demerphq
    my friends call me, usually because I'm late....