Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use named groups when performing a Regex.Replace()

Tags:

c#

regex

replace

How do I use named captures when performing Regex.Replace? I have gotten this far and it does what I want but not in the way I want it:

[TestCase("First Second", "Second First")] public void NumberedReplaceTest(string input, string expected) {     Regex regex = new Regex("(?<firstMatch>First) (?<secondMatch>Second)");     Assert.IsTrue(regex.IsMatch(input));     string replace = regex.Replace(input, "$2 $1");     Assert.AreEqual(expected, replace); } 

I want to match the two words with named captures and then use the (named) captures when performing the replace.

like image 709
Johan Larsson Avatar asked Oct 18 '12 19:10

Johan Larsson


People also ask

Can I use regex in replace?

How to use RegEx with . replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.

How do I specify a group in regex?

A backreference is specified in the regular expression as a backslash ( \ ) followed by a digit indicating the number of the group to be recalled. For example, the expression (\d\d) defines one capturing group matching two digits in a row, which can be recalled later in the expression via the backreference \1 .

How do I reference a capture group in regex?

If your regular expression has named capturing groups, then you should use named backreferences to them in the replacement text. The regex (?' name'group) has one group called “name”. You can reference this group with ${name} in the JGsoft applications, Delphi, .

How do you replace a character in regex?

The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.


2 Answers

Instead of "$2 $1", you can use "${secondMatch} ${firstMatch}".

There is a list of all the replacements you can do here.

Here is an abbreviated list:

$number - The captured group by number.

${name} - The captured group by name.

$$ - $ literal.

$& - Entire match.

$` - The input string before the match.

$' - The input string after the match.

$+ - The last group captured.

$_ - The entire input string.

like image 188
Kendall Frey Avatar answered Oct 16 '22 13:10

Kendall Frey


Simply replace with ${groupName}

[TestCase("First Second", "Second First")] public void NumberedReplaceTest(string input, string expected) {     Regex regex = new Regex("(?<firstMatch>First) (?<secondMatch>Second)");     Assert.IsTrue(regex.IsMatch(input));     string replace = regex.Replace(input, "${secondMatch} ${firstMatch}");     Assert.AreEqual(expected, replace); } 
like image 26
CaffGeek Avatar answered Oct 16 '22 13:10

CaffGeek