Regular Expression for Even Number

· November 3, 2008

Let me first be very clear - I’m not a RegExp-guy. I find them quite hard to understand at times and often take refuge in .NET code.

Also, I was very surprised to not find any hits for the search of “RegExp even numbers” - I thought that I would find numerous examples.

But after reading a bit here, I learned enough to create my first naïve regular expression - a regular expression to allow only even numbers:

^\d*?((0)|(2)|(4)|(6)|(8))$

Again, my apologies for being a newbie on this… but hey, it works.

I also found this resource very useful for trying out my expression.

Regular expressions are very powerful, but “with great power comes great responsibilities”… - that is, they are often hard to understand.

Thanks, Fredrik S, for the links.

Twitter, Facebook