I want to validate a numeric string that must only contain numbers ranging from 400 to 550.

$x = "401";  // valid 
$x = "551";  // invalid 

I want to only match the numbers between 400 and 550.

I tried the following pattern:

if(preg_match_all("/[400-550]+/",$x)); 

But it doesn't work.

What is the regex pattern for matching numbers 400-550?

share|improve this question
    
want to use "range" instead? php.net/manual/en/function.range.php – Funk Forty Niner May 5 '15 at 14:40
6  
Why do you want to do it via regex? – anubhava May 5 '15 at 14:40
1  
learning basic regex syntax would help. [...] defines a character CLASS, which matches a SINGLE CHARACTER in the target text, [400-500] is basically saying find one spot in the string where a character is either 4, 0, 1, 2, 3, 4, or 5. e.g. [400-500] is functionally identical to [0-5]. – Marc B May 5 '15 at 14:50
    
I'm with @anubhava here - unless you have a specific reason to use a Regex, this seems a little like an XY Problem (meta.stackexchange.com/questions/66377/what-is-the-xy-probl‌​em). The thing you seem to really be asking here is how to find if a number is in a numeric range, at least not assuming anything outside of what you've included in the question. – Mike May 5 '15 at 14:56
up vote 7 down vote accepted

A regex-way to validate this range:

\b(?:4[0-9]{2}|5[0-4][0-9]|550)\b

See demo. It will also work in larger texts since \b boundaries are used. In case of whole strings, use ^ and $ around instead of \b.

share|improve this answer
    
Stribiz , thank you. your solution is really helpful. – starkeen May 5 '15 at 15:18
1  
Feel free to mark as accepted :-) BTW, your original regex matched numbers consisting of digits from 0 to 5, of any length, because you put the numbers into a character class. 0-5 formed a range. – Wiktor Stribiżew May 5 '15 at 15:24

Just combine in_array() with range(), like this:

if(!in_array($x, range(400, 550)))
   echo "in ";
echo "valid";

Or just do a simple if statement, e.g.

if($x >= 400 && $x <= 550)
share|improve this answer
1  
It is seducting to do that but don't forget that range() creates an array (with 150 items). – Casimir et Hippolyte May 5 '15 at 14:51
    
@CasimiretHippolyte updated my answer. And if OP wants to exclude certain numbers from this range, he can use range() and just remove numbers from the array. – Rizier123 May 5 '15 at 14:52
1  
Bah! If he wanted this kind of thing, no one would have answered anyway. :) – Casimir et Hippolyte May 5 '15 at 14:55
    
Rizier , Your answer is correct but I did not not ask for a solution with php fuction Btw thanx for your cooperation. – starkeen May 5 '15 at 15:11
1  
@Starkeen You're welcome! (Not every time is the solution what OP wants, since OP may not know about other stuff) – Rizier123 May 5 '15 at 15:13

Should work :

^((4\d\d)|(5([0-4]\d|50)))$
share|improve this answer
1  
I think the capturing groups are not necessary here. I'd go for non-capturing ones. – Wiktor Stribiżew May 5 '15 at 14:46
1  
It may be faster, but capturing groups are an easy way to not make priority mistake – Blusky May 5 '15 at 14:47
    
@Blusky: yes but you have made a priority mistake (even with capturing groups). The anchors are not in factor with the 2 branches of your main alternation. – Casimir et Hippolyte May 5 '15 at 15:02
    
@CasimiretHippolyte where is it ? It seems valid for me ... – Blusky May 5 '15 at 15:57
    
@Blusky: It isn't, the start anchor only applies to numbers that begins with 4 and the end anchor only applies to numbers that begins with 5. In other words, the pattern will succeed with 4000 or 1550 (since ^400 and 550$ are found) – Casimir et Hippolyte May 5 '15 at 16:39

Try this:

 if(preg_match_all("/(4\d\d|5([0-4]\d|50))/",$x)) { ... }

Test: https://regex101.com/r/zU4vX8/2

share|improve this answer
1  
This will match up to 559 – anubhava May 5 '15 at 14:43
1  
This will match 551 wich is incorrect – Blusky May 5 '15 at 14:44
    
Fixed, now it will match correctly – Jean May 5 '15 at 14:46
    
@Jean: ) Unbalanced parenthesis: regex101.com/r/zU4vX8/1 – Wiktor Stribiżew May 5 '15 at 14:49
    
@stribizhev thanks! Fixed – Jean May 5 '15 at 15:07

Try /(([4][0-9]{2,2})|([5][0-4][0-9])|(550))/ (I haven't tested it). I dont know if there is an easier way.

share|improve this answer

Unless you have a reason to use a regex, just compare the numbers.

if ($x <= 550 && $x >= 400)

It'll be faster and easier to read than a regex or using array ranges.

share|improve this answer

The php code:

<?php
$subject = "550";
$pattern = '/4\d\d|5[0-4][0-9]|550/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
share|improve this answer
    
It does not match 501. – Wiktor Stribiżew May 5 '15 at 14:52
1  
Oops. Rectified the error in the eidt. – Jerry May 5 '15 at 14:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.