Sir, Please Step Away from the Regex. When to Use Regular Expressions in Code.
Introduction
Regular expressions are several things. They are:
- powerful.
- useful.
- sometimes overkill.
A difficult part for beginning programmers is when to use a regular expression in code. Sometimes, whipping up something with string functions is a better idea. You’re a smart coder if you can tell the difference from when you should use regular expressions and when you should not.
Signs that a regular expression is too much
1) Your regular expression is small and easy to understand
If you just made a really small, simple regular expression, it is possible that something similar could be achieved by other means. Below is an (almost extreme) example of this symptom.
/^3.+/
The above regular expression detects when a string starts with “3″. If you aren’t groaning at that regular expression, I would like to suggest that you direct yourself to documentation of string functions in whatever languages that you program in.
2) Diverse types of strings match your regular expression
The above example of “/^3.+/” fits this one too. Any string that starts with “3″ is a very diverse group of strings. Regular expressions were designed to match specific types of patterns. Now, a very general regular expression has its place in non-programming areas. A great example of this is find and replace.
3) You only need to check the start of a string
There are plenty of ways in computer languages to check the first few characters of a string. Our example of “/^3.+/” fits this again. You could just write code that just looks for the “3″.
Signs that a regular expression is not enough
1) If regular expressions aren’t robust enough for the task
Ever try to use regular expressions for XML parsing? Have you ever wanted to use regular expressions for XML parsing?
2) If the regular expression is more complex than any manual parsing
I remember once when I was looking through all sorts of regex tutorials, books, and so forth to find out how to do a certain task. That task could have been done easier with manual parsing.
Signs that a regular expression is perfect
1) You have to match something very specific.
Two great examples of this is using regular expressions to parse URLs and email addresses. For a URL, you can’t just write parsing code that looks for “http://” or “https://” at the beginning of a string. That would allow “http://$^%” to get through.
2) You would have to write, like, a billion lines of parsing code otherwise.
If regular expressions look like they will save you major developement time and headaches, go for it.
3) Everybody else is doing it.
Email addresses and URLs are examples for this one too. Everybody uses regular expressions in this situation because it’s the best way to do so. If you’re unsure on whether or not to use a regular expression, see if an experienced developer would do so.
Conclusion
Regular expressions have their place, but you have to make sure that you aren’t using them for way too simple tasks, or when there’s a smarter way to do so.
Credits: Thank i80and for some of the tips.
No related posts, but you might find these interesting
Posted in Best Practices, General Code, Programming Tips |








Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)