Regular Expressions and Numeric Comparisons

So let’s say you’re parsing through order numbers (as strings):

OD1004A
OD1004B
OD1108A
OE1108B
OE1108C
OE1109A
OE1148A
OE1149A
OE1151A

…and so on. And you want to find orders where the numeric portion is between 1100 and 1150, regardless of what the rest is.

Tempting as it is, this isn’t actually a regular expression problem, but requires a bit of numeric processing too.

We would need to parse the digits and then perform numeric comparison, e.g.:

$input = whatever(); # gets something like "OE1105A"

…then match with the following pattern:

preg_match("/w+(d+)w/", $input, $match);

…to store the digits in memory, and then:

if($match[1] >= 1100 and $match[1] <= 1150){...

to check to see if the number is in range.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s