Michael
Michael

Reputation: 8719

Regular expression in C#

I am trying to search and change all links in a html file I have.

so I want it to go throuhg and change <a href="whatever"
to <a href="mynewlink"

I can do it with visual studios find option using regular expression. But it keeps selecting too much of the string.

I have tried: <a href=".*"

but the problem is it will get the entire string until the last " (so if there for example:

<a href="www.google.com.au" id="myId">

it will select all the way up to the end of myID"

Upvotes: 2

Views: 78

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

The dot can also match a quote, and the asterisk makes it match as many characters as it can, so it'll match right past the end of the href attribute value.

Use <a href="[^"]*" instead. [^"] means "any character except quote", so it'll never match past the attribute value.

Upvotes: 4

Related Questions