Reputation: 4276
I have a .NET Regex that I'm using for a replace, and I want to update it.
string rxp = "(?<link>{link name=\"(?<name>[^\"]*)\" url=\"(?<url>[^\"]*)\"})";
input = Regex.Replace(input, rxp, "<a target=\"_blank\" href=\"$3\">$2</a>", RegexOptions.IgnoreCase);
This Regex will successfully capture {link name="Link 2" url="http://www.google.com"}
and convert it to <a target=\"_blank\" href="\http://www.google.com\">Link 2</a>
.
However, I want to handle the case where the quotes could come through as "
. i.e. {link name="Link 2" url="http://www.google.com"}
, but I still want this to render at the original output.
Currently, I have (?<link>{link name=[\"|<">](?<name>[^\"]*)[\"|<">] url=[\"|<">](?<url>[^\"]*)[\"|<">]})
which makes a match, but returns <a target=\"_blank\" href="\quot;http://www.google.com"\">quot;Link 2"</a>
.
For the life of me, I can't work out how to not include "
in the <name>
and <url>
values that get returned.
Upvotes: 0
Views: 63
Reputation: 71598
You don't quite need to use the inner (?: ... )
in (?:\"|(?:"))
; and use it like this:
(?:\"|")
Something you might do, however, to make sure there's no link with both "
and "
, you can actually capture the quotes and use it like this:
(?<link>{link name=(\"|")(?<name>(?:(?!\2).)*)\2 url=(\"|")(?<url>(?:(?!\4).)*)\4})
Basically, \2
is the backreference to the second capture (it will be either "
or "
) and then, instead of only [^\"]
, you will also be able to prevent the match of "
within the quotes themselves with (?:(?!\2).)*
. The same thing happens with the link.
If now you know that the quotes are always the same for both the name and the url, you can drop the second capture of quotes:
(?<link>{link name=(\"|")(?<name>(?:(?!\2).)*)\2 url=\2(?<url>(?:(?!\2).)*)\2})
Upvotes: 1
Reputation: 4276
A colleague taught me about passive matching.
Instead of []
for some of the parts, I should have been using ()
. And then to ensure those parts do not get returned, I can use (?:)
to prevent them being returned as matches for the replace.
My resulting regex is (?<link>{link name=(?:\"|(?:"))(?<name>[^\"]*)(?:\"|(?:")) url=(?:\"|(?:"))(?<url>[^\"]*)(?:\"|(?:"))})
Upvotes: 0