Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regex : Limit value of a string

by AppleFritter (Vicar)
on Oct 14, 2015 at 09:29 UTC ( [id://1144836]=note: print w/replies, xml ) Need Help??


in reply to [SOLVED] Regex : Limit value of a string

The following regex:

/(^gi.*)([1-8])(\/)(0)(\/)([1-9]|[1-3][0-9]|4[0-8])/i

aside from not anchoring to the end of the string, as hippo has pointed out, explicitely attempts to match numbers from 1 to 48 only, so using this to extract said number and then checking to see whether it's greater than 48 won't work. It won't be; if it were the regex wouldn't match in the first place.

As an aside, is there a reason you're explicitely capturing the backslashes and the zero? I'm assuming not; I've removed those captures below, but if you need them after all for some reason just put them back in.

Now, asides aside, since you don't want to explicitely check whether the number's greater than 48, you'll just want to match the regex against the user's input, and only proceed if it does (knowing then that the number does not exceed 48). And I'd do that anyway, since if the input you're matching against is supplied by the user, the rest of it may also not be what you expect, and explicit matching will make sure you're getting precisely what you expect.

So just do something like this:

# ... if($input =~ m/^(gi.*)([1-8])\/0\/([1-9]|[1-3][0-9]|4[0-8])$/i { my ($string, $digit, $number) = ($1, $2, $3); # e.g. "GigabitEther +net", "1" and "48"; $number will be at most 48 # ... }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-23 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found