I’m trying to make something pretty simple, but I simply suck at regular expressions.
My goal is to replace :
<a href="http://www.google.com">Link To Google</a>
To :
<b>Link To Google</b>
In java.
I tried this :
String input = "<a href=\"http://www.google.com\">Link to Google</a>";
String Regex1 = "<a href(.*)>";
String Regex2 = "</a>";
String output = test.replace(Regex1, "<b>");
output = test.replace(Regex2, "</b>");
But the first Regex1 is not matched with my input. Any clue ?
Thanks in advance!
It matches just fine, even tho it’s wrong, and you should not use regex to parse HTML.
You want to make the second replace on the result of the first replace, not the original string:
You can make it work for your example by using:
Which makes the quantifier ungreedy. But this expression will break very easily for the slightest changes in the input HTML, which is (one of the reasons) why you should’t use regex to work with HTML.
Some simple examples the above regex would not work for: