c# - Regex Replace with named group and everything else around -
Imagine that I had a regex that had to capture the HTML link and replace it with another link.
Rexax will look something like this:
& lt; A. + Href = "(? 'Url'.)". *? & Gt;
This will match the HTML tags:
& lt; A href = "http://www.google.com" & gt;
Or this:
& lt; One goal = "_ blank" href = "#top" & gt;
If I now call
regex.Replace (new MatchEvaluator (ReplaceUrl))
I will get Match the parameters and within the named group.
But match There are only 3 items in the group collection.
How do I keep the whole match together, the result will be
Thanks to Jones (see below I finally understood the meaning of the index in the group collection,
All anonymous groups start with only 1. They Because they are in the regex pattern.
Therefore, my replacement method is based on the (uncompressed rijks which I will actually optimize):
Private static string Reverse Euro (match match) {Return (match group [1]. Value + "http: // www. EvilRool.devil" + Match.Group [2] .Value;}
(& lt; a. +? Href = ") ([^"] + "
Then plug in those places:
var pattern = @ "(& Lt; a. +? Href = "") ([^ "]] +" ("" [^ & gt; * & gt;) "; var str = @" & lt; a target = "" _ blank "" Href = "#top" "var newUrl =" http: //www.stacko verflow.com "; var newTag = Regex. Change (str, pattern," $ 1 "+ newUrl +" $ 3 ");
(I've changed your base regex)
Comments
Post a Comment