Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

PHP/REGEX: Number greater than 12 in specific place

Using PHP and REGEX, how can I test if a string contains a number greater than immediately after "Item_"?

testAbove12('xyz/abc/Item_12.txt'); // true
testAbove12('xyz/abc/Item_5.txt'); // false
testAbove12('hello world Item_234.txt'); // true
testAbove12('ABC Item_234 HELLO world'); // true
testAbove12('xyz/abc/Item.txt'); // false

Open in new window

SOLUTION
Avatar of Member_2_4694817
Member_2_4694817

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hankknight

ASKER

Why do I need all this?
[2-9][0-9]|1[2-9]|11[0-9]

Is there a way to do something like
[0-9+]
?
\d+ means one or more digits, it is shorthand for [0-9]+

Try this:

function testAbove12($str) {
    return preg_match('@/Item_(\d+)@',$str,$m) ? ((int)$m[1]>=12) : false;
  }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial