I don't know about you, but I find regluar expressions quite difficult. Apart from a very simple, I almost always have to look it up or use a reference book.
Yesterday, one of my colleagues needed a regex for filtering on Google Analytics. The value needed to be greater than 1200 to be valid.
In case you are looking for a regex for something similar, I though I would post it so you can alter it for your needs.
Regex greater than 1200
^[1-9][2-9](?!00$)[0-9][1-9]?\d+$
I used this tool to test it.
And this is the tool's English interpretation of what the regex is doing:
-
^ assert position at start of the string
-
[1-9] match a single character present in the list below
-
1-9 a single character in the range between 1 and 9
-
-
[2-9] match a single character present in the list below
-
2-9 a single character in the range between 2 and 9
-
-
(?!00$) Negative Lookahead - Assert that it is impossible to match the regex below
-
00 matches the characters 00 literally
-
$ assert position at end of the string
-
-
[0-9] match a single character present in the list below
-
0-9 a single character in the range between 0 and 9
-
-
[1-9]? match a single character present in the list below
-
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
-
1-9 a single character in the range between 1 and 9
-
-
\d+ match a digit [0-9]
-
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
-
-
$ assert position at end of the string
-
1-9 a single character in the range between 1 and 9
-
2-9 a single character in the range between 2 and 9
-
00 matches the characters 00 literally
-
$ assert position at end of the string
-
0-9 a single character in the range between 0 and 9
-
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
-
1-9 a single character in the range between 1 and 9
-
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
Summary
The key part to this is the look aheadĀ (?!00$), this makes sure that it won't end with 00 after the first 2 digits. Without this in there, it would say 1200 is valid, which violates the rule of greater than
Anyway, I hope someone finds this useful one day. Or me 6 months time when I've forgotten and need something similar.