Better regex answer to FMTEYEWTK question "Is it a number?" 
Author Message
 Better regex answer to FMTEYEWTK question "Is it a number?"

I've been trying to match strings which contain numbers which may or may
not
be separated by commas:

    , 10 20.5, -30.35e+02, ,

When I tried using a regex of the form:

    /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/

(from the FMTEYEWTK series) to handle the numeric parts, I got a regex
error message.  Here's what I tried:

    $_ = '    , 10 20.5, -30.35e+02, ,';

    print "good\n" if
        m/([+-]? (?=\d|\.\d) \d* (\.\d*)? ([Ee] [+-]? \d+)? \s*
(,\s*)*)+/x;
                 ^^^^^^^^^^^^^^^^^^^^^^^^

and the message was:

    /([+-]? (?=\d|\.\d) \d* (\.\d*)? ([Ee] [+-]? \d+)? \s* (,\s*)*
)+/:     \
    regexp *+ operand could be empty (...)

If I change the match part to:

    print "good\n" if
        m/([+-]? (\d+(\.\d*)? | \.\d+) ([Ee] [+-]? \d+)? \s* (,\s*)*
)+/x;
                 ^^^^^^^^^^^^^^^^^^^^^

my script worked.  It seems that (?=\d|\.\d) portion in the first
attempt causes (or fails to prevent) the above regex error message.

This may or may not be a bug in the regex compiler; I'm not sure.  If
it's
not, then a better way to match numbers with a regex would be

    m/^[+-]?(\d+(\.\d*)?|\.\d+)([Ee][+-]?\d+)?$/;

which differs from the regex in the FMTEYEWTK series.

Brett

P.S. By the way, the idea for the "better" (possibly) regex comes from
"Mastering Regular Expressions".

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Brett W. Denner                                    Lockheed Martin TAS

748            
 (817) 935-1144 (voice)                             Fort Worth, TX 76101
 (817) 935-1212 (fax)                               MZ 9333



Fri, 25 Feb 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. "Simple" regex question

2. I am an "antispam spammer"?

3. "character class ""bug""

4. Group capturing and "nested" regex

5. Perl 4/5 regex "bug" revisited

6. regex for "no character repeating itself"

7. Group capturing and "nexted" regex

8. "Unmatched () in regex" error

9. RegEx for matching Mbx "From " delimeter

10. regex for one "two three" four

11. Understanding the regex in "commify" (perlfaq5)

12. global regex in an "if" statement

 

 
Powered by phpBB® Forum Software