nurdyguy nurdyguy - 1 year ago 44
C# Question

Regex- replace match with middle between delimiters

I have some html that I am trying to parse out which has some merge variables. The variables are delimited, starting with

and ending with
. For example:
What I'm trying to do is replace the entire match with just the "VALUE".

becomes just

I tried using MSDN's substitution examples using

string input = "aaaaa%nomatch%bbb%x.FOO%ccc%x.BAR%ddddd%%";
string pattern = @"%x.(.*?)%";
string substitute = "$&";

string result = Regex.Replace(input, pattern, substitute);

and I tried it with a couple of different substitution strings but none worked. I realize the
just replaces it with itself so it doesn't really do anything but I don't know how to replace the entire match with just the middle. Does it use some sort of nested regex?

For that string, I would expect the result to be


Here the
is replaced with
replaced with
but the other
don't have the
so they are not matches and are unchanged.

Answer Source

It is true that $& is a backreference to the whole match. The $1 backreference will replace with the value captured into the first capturing group.





And replace with $1 backreference.


var result = Regex.Replace(input, @"%x\.([^%]*)%", "$1");

See more about Substitutions in Regular Expressions at MSDN.

To return FOO or BAR, i.e. to return the contents of the first capturing group, use

var results = Regex.Matches(input, @"%x\.([^%]*)%")
      .Select(p => p.Groups[1].Value)

See the regex demo.