I have a HTML string and want to replace all links to just a text.
E.g. having
Some text <a href="http://google.com/">Google</a>.
need to get
Some text Google.
What regex should I use?
I have a HTML string and want to replace all links to just a text.
E.g. having
Some text <a href="http://google.com/">Google</a>.
need to get
Some text Google.
What regex should I use?
Several similar questions have been posted and the best practice is to use Html Agility Pack which is built specifically to achieve thing like this.
<a href=.*?>... will fail even for simple, valid HTML. Allowing .*? is naïve even by the low, low standards of regex; for example a simple difference like the close-tag being </a > and you've just matched a big stretch of document across multiple links by mistake. Plus, of course, the hundred other constructs that'll trip this over. Do yourself a favour. Use an HTML parser. It's what they're there for.I asked about simple regex (thanks Fabrian). The code will be the following:
var html = @"Some text <a href="http://google.com/">Google</a>.";
Regex r = new Regex(@"\<a href=.*?\>");
html = r.Replace(html, "");
r = new Regex(@"\</a\>");
html = r.Replace(html, "");