Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex replacement capture followed by digit

Tags:

.net

regex

I am trying to get a Regex replacement working to update my AssemblyInfo.cs files, so I have:

Regex.Replace(     contents,      @"(\[assembly: Assembly(File)?Version\("").*(""\)\])",      "$1" + version + "$3" ); 

The problem is that version is something like "1.5.3.0", so that when the replacement is evaluated it is seeing "$11.5.3.0$3" and is presumably looking for the eleventh captured group because it is coming out with:

$11.5.3.0")] 

If is stick a space after the $1 it works fine. What do I need to put in there to escape the second digit without actually inserting the character?

like image 472
derkyjadex Avatar asked Aug 12 '10 10:08

derkyjadex


People also ask

How do you reference a capturing 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 substitute in regex?

Substitutions are language elements that are recognized only within replacement patterns. They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters.

What does capture mean in regex?

capturing in regexps means indicating that you're interested not only in matching (which is finding strings of characters that match your regular expression), but you're also interested in using specific parts of the matched string later on.

What is non capturing group in regex?

Non-capturing groups are important constructs within Java Regular Expressions. They create a sub-pattern that functions as a single unit but does not save the matched character sequence. In this tutorial, we'll explore how to use non-capturing groups in Java Regular Expressions.


1 Answers

Use ${1} instead of $1. This is also the substitution syntax for named capturing group (?<name>).

Here's a snippet to illustrate (see also on ideone.com):

Console.WriteLine(Regex.Replace("abc", "(.)", "$11"));        // $11$11$11 Console.WriteLine(Regex.Replace("abc", "(.)", "${1}1"));      // a1b1c1 Console.WriteLine(Regex.Replace("abc", "(?<x>.)", "${x}1"));  // a1b1c1 

This behavior is explicitly documented:

Regular Expression Language Elements - Substitutions

Substituting a Numbered Group

The $number language element includes the last substring matched by the number capturing group in the replacement string, where number is the index of the capturing group.

If number does not specify a valid capturing group defined in the regular expression pattern, $number is interpreted as a literal character sequence that is used to replace each match.

Substituting a Named Group

The ${name} language element substitutes the last substring matched by the name capturing group, where name is the name of a capturing group defined by the (?<name>) language element.

If name does not specify a valid named capturing group defined in the regular expression pattern, ${name} is interpreted as a literal character sequence that is used to replace each match.

like image 162
polygenelubricants Avatar answered Sep 26 '22 09:09

polygenelubricants