Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
I have a file containing about 2000 lines such as this:
<nobr> <a href="../Carbon_Monoxide_Poisoning_Prevention.htm"><b>poisoning - prevention</b></a></nobr><br>
<nobr> <a href="../Carbon_Monoxide_Symptoms.htm"><b>symptoms</b></a></nobr><br>
1.) the URL is ALWAYS in the form of ../foo.html
2.) the display name is SOMETIMES enclosed with <b> ... </b> tags, and sometimes not.
3.) each line in the file contains up to four that I need to count and flag as spaces. These will EVENTUALLY be used to format indents, so I need to capture the information somehow.
I need to have the hyperlink, display name and number of spaces name in a delimited flat file as follows (based on the above data):
../Carbon_Monoxide_Poisoning_Prevention.htm,poisoning - prevention,4
../Carbon_Monoxide_Symptoms.htm,symptoms,4
. While I can parse this through a whole mess of String, substring, and if statements, that seems to be more cumbersome than it needs to be. I was investigating Regex (my first time doing so), but am a little unclear on some of the syntax; I learn best seeing a code sample similar to my applications, but have not been able to find examples of anything that quite fits.
Any help would be appreciated!
You can only grab one thing at a time, all the URLs at once, display name, or the spaces. I would not use regular expressions per say to do this, but here is how I would go about it if I absolutely had to use regex:
To grab the url in a line:
\.\./.*\.html?To grab the display name:
(?<=("|b)>)[a-ZA-Z].+?(?=(</(a|b)))To grab the spaces (simply):
I would first split the file by the
<br>tag to get the individual lines. And the run the regex above to pull out the url, display name, and spaces and combine them in a delimited output. I’m sure Java has apreg_match_allequivalent to match all patterns found (would be useful for the spaces & counting them)Note that these patterns were tested in Sublime Text and probably will not work in Java without a little tweaking. I can modify my answer later to include the Java if needed, but for a one-off thing like this you may be better off using Python or some other scripting language.
Best of luck!