Matching numerical range with Regular Expression

Regular Expression is powerful for matching texts with alphabets but numerical ranges. For example, a to z can be matched by [a-z] but 1 to 22 can not be matched by [1-22]. Intuitively, we can numerically assign the range we want to match by (1|2|3|…|21|21|22). Numerical assignment is precisely but we need a more elegant and flexible way.

First, I’ve written the pattern, ([1-9]|1[1-9]|2[0-2]), but it fails to match 20. The reason is the first character of 20 is matched by the pattern [1-9] and the function exit. To fix this problem, the priority for matching should be altered and the pattern becomes: (1[0-9]|2[0-2]|[1-9]). It matches the texts from 1 to 22 successfully but the sub-string within 010 which is unexpectedly.

According to Matching Numeric Ranges with a Regular Expression, we can design the pattern with word boundary (\b). The final pattern is designed

\b(1[0-9]|2[0-2]|[1-9])\b

where the texts (1, 2, 3, …, 20, 21, 22) can be matched except for sub-strings of the text (eg. 12345).

For online testing, I suggest regexpal.com. It’s very useful. Enjoy it. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *