There are times in every programmer’s life when we need to write a regular expression. It sounds really, really simple but believe me – it’s not easy at all. If you don’t deal with regular expressions day-to-day it can be truly painful process. How to make it simpler?
Fortunately for us all, there are some useful tools on the net that make generating regular expressions much less painful. My favourite one is txt2re generator, which allows us to create expression extracting given data by simply clicking on correct parts of data string.
Usage is very simple and intuitive: first enter string you are trying to process – it may be output of some website or whatever you are trying to extract. Then press “Show Matches” and you should see the string parsed into colorful pieces. Here is where the magic happens: by clicking left or right part of any chunk we can make adjustment to matching expression. Left part always uses generic match (word, variable, character etc) while right part gives us possibility to match against specific data (letters and strings).
While there are several limitations to this method, I tested it with real problems and it works fine! If generated regular expression needs further tuning, it is very easy due to way how expression is generated. Txt2re can really save you day!