Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is $1 and $2 in Regular Expressions?

Tags:

c#

regex

I have simple question regarding regular expressions in C#.

What is $1 and $2 in C# regular expression?

Does both come under groups?

like image 248
Viki888 Avatar asked Oct 02 '13 15:10

Viki888


People also ask

What is the meaning of $1 in regular expression?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group. For more information about numbered capturing groups, see Grouping Constructs. All digits that follow $ are interpreted as belonging to the number group.

What is $1 in replace JS?

In your specific example, the $1 will be the group (^| ) which is "position of the start of string (zero-width), or a single space character". So by replacing the whole expression with that, you're basically removing the variable theClass and potentially a space after it.

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

What is $1 jQuery?

$1 refers to the first match, $2 to the second one. The expected contents of the num string is thus 11222,333 after this bit of code. Show activity on this post. This is how jQuery users a variable called $ as an alias for the jQuery object.


2 Answers

That is values of captured groups by index. $1 is a first captured group, and $2 is a second captured group. As David pointed, these values used in replacement patterns.

string input = "Hello World";
string result = Regex.Replace(input, @"(\w+) (\w+)", "$2 $1");

Output: World Hello

like image 158
Sergey Berezovskiy Avatar answered Oct 15 '22 21:10

Sergey Berezovskiy


These are substitutions. Specifically numbered group substitutions. From the documentation:

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. For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group. For more information about numbered capturing groups, see Grouping Constructs in Regular Expressions.

Capturing groups that are not explicitly assigned names using the (?) syntax are numbered from left to right starting at one. Named groups are also numbered from left to right, starting at one greater than the index of the last unnamed group. For example, in the regular expression (\w)(?\d), the index of the digit named group is 2.

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.

The following example uses the $number substitution to strip the currency symbol from a decimal value. It removes currency symbols found at the beginning or end of a monetary value, and recognizes the two most common decimal separators ("." and ",").

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\p{Sc}*(\s?\d+[.,]?\d*)\p{Sc}*";
      string replacement = "$1";
      string input = "$16.32 12.19 £16.29 €18.29  €18,29";
      string result = Regex.Replace(input, pattern, replacement);
      Console.WriteLine(result);
   }
}
// The example displays the following output: 
//       16.32 12.19 16.29 18.29  18,29
like image 37
David Heffernan Avatar answered Oct 15 '22 22:10

David Heffernan