nurdyguy nurdyguy - 3 months ago 9
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

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

So,
%x.VALUE%
becomes just
VALUE
.

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?

Edit:
For that string, I would expect the result to be

aaaaa%nomatch%bbbFOOcccBARddddd%%


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

Answer

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.

Use

%x\.(.*?)%

or

%x\.([^%]*)%

And replace with $1 backreference.

C#:

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\.([^%]*)%")
      .Cast<Match>()
      .Select(p => p.Groups[1].Value)
      .ToList();

See the regex demo.

Comments