Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capturing group in regex [duplicate]

I am exploring capturing groups in Regex and I am getting confused about lack of documentation on it. For ex, can anyone tell me difference between two regex:

/(?:madhur)?/ 

and

/(madhur)?/ 

As per me, ? in second suggests matching madhur zero or once in the string.

How is the first different from second ?

like image 685
Madhur Ahuja Avatar asked Jun 21 '11 00:06

Madhur Ahuja


People also ask

How do Capturing groups work in regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

What is first capturing group in regex?

First group matches abc. Escaped parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex.

How do you repeat in regex?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

What is capturing group in regex Javascript?

Groups group multiple patterns as a whole, and capturing groups provide extra submatch information when using a regular expression pattern to match against a string. Backreferences refer to a previously captured group in the same regular expression.


2 Answers

The first one won't store the capturing group, e.g. $1 will be empty. The ?: prefix makes it a non capturing group. This is usually done for better performance and un-cluttering of back references.

In the second example, the characters in the capturing group will be stored in the backreference $1.

Further Reading.

like image 155
alex Avatar answered Oct 08 '22 20:10

alex


Here's the most obvious example:

"madhur".replace(/(madhur)?/, "$1 ahuja");   // returns "madhur ahuja" "madhur".replace(/(?:madhur)?/, "$1 ahuja"); // returns "$1 ahuja" 

Backreferences are stored in order such that the first match can be recalled with $1, the second with $2, etc. If you capture a match (i.e. (...) instead of (?:...)), you can use these, and if you don't then there's nothing special. As another example, consider the following:

/(mad)hur/.exec("madhur");   // returns an array ["madhur", "mad"] /(?:mad)hur/.exec("madhur"); // returns an array ["madhur"] 
like image 29
brymck Avatar answered Oct 08 '22 21:10

brymck