From Grundy
Jump to: navigation, search

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.
  • RegExr
  • regex 101
  • Regexpal - A great tool to check your regular expressions before actually implementing them in your code.

Practical Regex

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 /<a[^>]*>([^<]+)<\/a>/g.
  • 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.

Fun Stuff