tags:

views:

53

answers:

2

I've created a regular expression that matches any numeric value (with leading or trailing spaces) that contains ascii or arabic characters. Now I need to modify it to only match a certain range of values (e.g. 1900-1950).

(^\s*-?\d+\s*$)|(^\s*-?[\u0660-\u0669]+\s*$)

I'm pretty stuck. Anyone have any suggestions?

+4  A: 

Seems that's really a bad use for a regular expression - a much easier (and simpler to maintain!) approach would be to match any number, and then do a simple comparison in the code to check if it's within the range you want.

As a side note, do you really want to include the spaces in the match? Wouldn't it make more sense to move the \s* outside of the parens?

zigdon
You're right, moving the spaces outside of the parens is a good call. I also think doing the range check in code is a good idea. This started as just a check for numeric values, which was great for a regular expression. When the range constraint needed to be added I didn't even think to consider another approach.Thanks for the suggestion!
Kevin Babcock
+1 for pointing out that every problem is not a nail.
Amarghosh
A: 

I'm not sure about what you posted, but if I'm understanding you right you have a character class that is acceptable between digits of a numeric value, so that "19fhuerfn4dsfjkw0" would match as it is 1940 with characters in between?

So given the character class that you want to match ([class]), you could just do this?

1[class]9[class][0-4][class][0-9]

If your number range isn't perfectly aligned with base 10, you may need to modify with a couple ors...like to make this one be 1900-1950 instead of 1900-1949 you would need to include a separate case for ending with 50.

Edited to add: It would maybe be easier to just parse through the input character by character. Ifg it's not a digit, strip it out. When the non-digits are stripped out, parse the remaining characters as an integer and do your range check. Then check if all the stripped out characters match your accepted class with a regexp.

19sdfh2djf3 \/ sdfhdjf + 1923 \/ match(sdfhdjf) && (1900 <= 1923 <= 1950)

Brian Schroth