Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Need a regex..

by littlemonk (Sexton)
on May 21, 2012 at 13:55 UTC ( #971616=note: print w/replies, xml ) Need Help??


in reply to Re: Need a regex..
in thread Need a regex..

it should match non-decimal value also ...example 12,123, and it should not match more than 3 digits before and after decimal..i have one regex(/^\- \+{0,1}0-9{0,3}(\.){0,1}0-9{0,3}\%{0,1}$/) but its accepting 1234(more than 3 digits before decimal) ... plz help me

Replies are listed 'Best First'.
Re^3: Need a regex..
by kennethk (Abbot) on May 21, 2012 at 14:33 UTC

    Please wrap code it <code> tags to keep things legible. As you can see, your character classes got linkified.

    1234 is passing because it's being read as (123)(4). You should set the whole part following and including the decimal in a non-capturing group with a conditional: /^[-+]?[0-9]{0,3}(?:\.[0-9]{0,3})?%?$/ I've also removed a bunch of unnecessary escaping, and changed your {0,1} to the shorter ? synonym.

    A read through of perlretut may be helpful.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re^3: Need a regex..
by Neighbour (Friar) on May 21, 2012 at 14:40 UTC
    Edit: Lost the race to Re: Need a regex.., which is also slightly better :)
    Try a more grouped approach:
    /^([+]|[-])?[0-9]{0,3}(\.[0-9]{1,3})?$/
    First the start of the line: ^
    then an optional + or - sign, with the + enclosed in [] to avoid interpretation: ([+]|-)?
    then 0-3 digits: [0-9]{0,3}
    then an optional group of a period followed by 1-3 digits, with the period escaped in another way to avoid interpretation: (\.[0-9]{1,3})?
    and finally the end of the line: $

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971616]
help
Chatterbox?
[Corion]: Hmmm - and now that I look at it, the gallery I'm using doesn't produce non-Javascript compatible links in the sense that hotlinking to an image will only work for Javascript enabled...
[Corion]: On the other hand, maybe supporting non-Javascript isn't that much a priority, and I'm not exactly sure how I could make it work for both kinds of browsers without server-side magic
[Corion]: Should I ever come across far too much money, this would become my evil maniac lair ;)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-02-27 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (377 votes). Check out past polls.