Regex or regular expressions are best described by the following internet joke.
I had a problem. I decided to use regex. Then I had two problems.
Well, jokes apart, it is an extremely useful tool to search parts of a string.
Useful Learning Resources
- Automate the Boring Stuff, Chp 7 - An excellent chapter on Regular Expressions in Python, covering very basic ideas and links to learn more about regular expressions.
- Regex Quickstart - A great introduction to Regex. This website is comprehensive and covers regular expressions in great detail.
- regex 101
- Regexpal - A great tool to check your regular expressions before actually implementing them in your code.
The quote we started the article with isn't always a joke. Regular Expressions are dangerous. Keep the following in mind while trying to use a regular expressions -
- Try to Avoid Regex - In many situations, you can completely avoid regular expressions by solving the problem in a better way. Always prefer good libraries, irrespective of the fact that they may be using regular expressions inherently. A great example of this is while parsing HTML in Python. One must use BeautifulSoup's APIs and not
- Search StackOverflow First - If you cannot find a library performing the desired task, always look for StackOverflow's regex answers. You will find excellent solutions for common problems like email validation, URL validation etc. Write your own regular expression (this is fun, but not recommended) only if everything else fails. Also, make sure you are careful while doing this. Always use Regexpal.
- Avoid using . - Please read the "Use the Dot Sparingly" section on Dot.
- Regex are Impossible to Read - Try to add a comment explaining a regular expression whenever you are forced to use one.
- How a Bad Regex Brought StackOverflow Down - Stack Exchange Outage Postmortem