Today, I relearned to use regular expression to extract certain data out of well formed HTML pages. There are many books and articles with sample code, such as this one, so I’ll just briefly note down a few things here.
.*? means any arbitrary number of characters. This essentially tell the parser to ignore or include everything. This only becomes useful when you add additional regular expression before or after
.*?. For example,
.*?abc tells regular expression processing engine to look for anything that ends with abc. Here is another example:
\"(.*?)\" will result a match with anything that are surrounded with double quotes. Note that double quote characters are escaped. This essentially is the basic understanding that I needed to define my own regular expression to parse a well formed HTML page.
In my Java code. I first created a substring of the input string, which eliminated anything before and after the data I am interested to extract. Next I called compile method on Pattern class with the regular expression string I created. Then I called the matcher method on the pattern object I just created with the input substring, which should return a matcher object. To extract the data, I called
group() methods. For sample code, see the link in the first paragraph of this blog.