Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Matching floats according to perlfaq4

by pope (Friar)
on Sep 25, 2002 at 11:47 UTC ( #200602=note: print w/replies, xml ) Need Help??


in reply to Matching floats according to perlfaq4

The second regex matches .1.1 (add this to the test case), which is wrong.

-- pope who is not a pope, or the pope

  • Comment on Re: Matching floats according to perlfaq4

Replies are listed 'Best First'.
Re: Re: Matching floats according to perlfaq4
by jmcnamara (Monsignor) on Sep 25, 2002 at 12:46 UTC

    You are right. A look ahead is required in the second regex:     /^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee][+-]?\d+)?$/

    --
    John.

Re: Re: Matching floats according to perlfaq4
by Anonymous Monk on Sep 25, 2002 at 12:49 UTC
    /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/

    ==

    /^[+-]?(\d+\.\d*|\d*\.\d+)(e[+-]?\d+)?$/i

    The look-ahead is to ensure that a '.' on its own isn't matched, but that (eg) '.04' and '4.' is. I'm not sure how correct the latter form is. The e+3, E-2 etc bit may or may not match; I don't think (example) '1.2e' on its own is meaningful.

      ...and of course I meant

      /^ [+-]? ( \d+ (\.\d*)? | \.\d+ ) (e[+-]?\d+)? $/ix

      ... really (whitespace added for readability)

      Of course it wont match 1.2e so that isnt a worry. And 4. certainly is meaningful (to perl anwyay). Try it...

      Also as I pointed out in my earlier reply the two regexes are _not_ == to each other. They are similar in that they match or reject similar data, however the parts that they match and the way they get captured for later use are radically different.

      For instance

      /([A-Za-z_0-9])(\w)(\w)/
      matches/rejects the same data as
      /(\w{3})/
      But the utility of the two is totally different....

      :-)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://200602]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2020-01-27 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?