Regex match, check number values are within a range

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Regex match, check number values are within a range

Post by batfastad »

Hi everyone
I've been slowly getting used to regular expressions over the past 6 months. It was always something I'd put off learning, and am now finding them very useful... thanks to Chris Corbyn's excellent tutorial on here.
I'm now stuck on a particular regex, I think it might be something advanced that I've not learnt yet.

Is there a way to get a particular group of numbers are within/outside of a numerical range?

So I have the following regex pattern:

Code: Select all

preg_match_all('#[0-9]{2}[a-zA-Z]{3,8}[0-9]{2}&{7}[0-9]{2}#', $due_date, $matches);
And I want to check that the middle group of numbers is between 11 and 58
Is this possible within regular expressions?

As an alternative I can just split the string apart and test it in PHP.
Just wondered if there was a way to do this with regular expressions?

Cheers, B
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Regex match, check number values are within a range

Post by mintedjo »

I'm not exactly an expert but i think the only way to do this using only regex would be the following

Code: Select all

#[0-9]{2}[a-zA-Z]{3,8}[b](1[1-9]|5[0-8]|[2-4][0-9])[/b]&{7}[0-9]{2}#
you would have to say that it can only be:
1 followed by (1-9)
OR
(2-4) followed by (0-9)
OR
5 followed by (0-8)

Like I said I`m not an expert so maybe theres something I dont know about that would be more appropriate.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex match, check number values are within a range

Post by prometheuzz »

mintedjo wrote:I'm not exactly an expert but i think the only way to do this using only regex would be the following

Code: Select all

# ... [b](1[1-9]|5[0-8]|[2-4][0-9])[/b] ... #
You're absolutely right! Well done.
Personally, I'd like to keep things from small to large, so I'd write it like this: (1[1-9]|[2-4][0-9]|5[0-8]), but that's just eye candy (or my compulsive nature to keep things in order... or both).
;)
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Regex match, check number values are within a range

Post by mintedjo »

prometheuzz wrote:Well done
xD
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: Regex match, check number values are within a range

Post by batfastad »

Ok fair enough. Just thought there might be some regex syntax I didn't know about.
I'll keep the testing of the number ranges within PHP I think as it's a bit more flexible.

Thanks for the info though
Cheers, B
piyushj
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2009 10:40 am

Re: Regex match, check number values are within a range

Post by piyushj »

regular expression for the only strings that are not generated over {a,b} by the expression (a+ b)* a (a+b)*
reasoning please
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Regex match, check number values are within a range

Post by prometheuzz »

piyushj wrote:regular expression for the only strings that are not generated over {a,b} by the expression (a+ b)* a (a+b)*
reasoning please
Stop hijacking other people's threads. Again: try posting a new thread in the regex forum. Note that the "question" in it's current form, is pretty meaningless and is not likely to get any helpful answers, I think.
Post Reply