jurchiks jurchiks - 1 year ago 97
PHP Question

php regex finding exact number ranges

Is there any way to check for a number in a string within an exact range using purely regex?

What I mean is that I want to check if a string contains a number that ranges, for example, from 1 to 31 in VALUE, not length.

Answer Source

The numbers of the range from 1 to 31 can be split into two groups: one digit numbers (1–9) and two digit numbers (10–31). And the latter can further be split into whole decades (10–29) and partial decades (30–31). These can be expressed as follows:


And to match only whole numbers and not just parts of them, you could use look-around assertions:


Here (?<!…) means not preceded by and (?!…) not followed by .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download