Should I avoid regular expressions?

If you can easily do the same thing with common string operations, then you should avoid using a regular expression.

In most situations regular expressions are used where the same operation would require a substantial amount of common string operations, then there is of course no point in avoiding regular expressions.


Don't avoid them. They're an excellent tool, and when used appropriately can save you a lot of time and effort. Moreover, a good implementation used carefully should not be particularly CPU-intensive.


Overhyped? No. They're extremely powerful and flexible.

Overused? Absolutely. Particularly when it comes to parsing HTML (which frequently comes up here).

This is another of those "right tool for the job" scenarios. Some go too far and try to use it for everything.

You are right though in that you can do many things with substring and/or split. You will often reach a point with those where what you're doing will get so complicated that you have to change method or you just end up writing too much fragile code. Regexes are (relatively) easy to expand.

But hand written code will nearly always be faster. A good example of this is Putting char into a java string for each N characters. The regex solution is terser but has some issues that a hand written loop doesn't and is much slower.

Tags:

Regex