Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple regex

Status
Not open for further replies.

johno77

Technical User
Nov 1, 2003
40
GB
Can someone tell me if i can search for an integer in the range 0-100 by the regex [0-100]

I know its a simple question but i need to make sure

thanks
 
No. What you are using is not really a range operator. Rather it is a pattern class. When you use [ ] you are telling the parser to find any character that appears between the [ ]. But it does not look for them in that order. It is just looking to see if any of those exist. As is, the regex will look for one single character out of the following characters:

0
-
1

What I would do is look for 1 to 3 digits, save them into a string, and then do a numeric comparison to see if they are <= 100.

my $string = &quot;the is the number 87&quot;;
my ($num) = $string =~ /([0-9]{1,3})/;
if ($num <= 100) {
....
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top