Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex $1, $2, etc

I've been trying to do some regex operations in PHP, and I'm not very skilled in this area. It seems that when I use a regex function like preg_replace on a string, I can access the regex-replaced strings by some sort of variables named $1, $2, and so on. What is this called and how can I use it?

like image 352
Waleed Khan Avatar asked Dec 26 '10 16:12

Waleed Khan


People also ask

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.

What is $1 and $2 in JavaScript?

A piece of JavaScript code is as follows: num = "11222333"; re = /(\d+)(\d{3})/; re. test(num); num. replace(re, "$1,$2");

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What is difference [] and () in regex?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.


2 Answers

These are known in regex terminology as backreferences (more on that here). You use them to refer to capture groups (or subpatterns, surrounded by ()) within your regex or in the replacement string.

An example:

/*   * Replaces abcd123 with 123abcd, or asdf789 with 789asdf.  *   * The $1 here refers to the capture group ([a-z]+),  * and the $2 refers to the capture group ([0-9]+).  */ preg_replace('/([a-z]+)([0-9]+)/', '$2$1', $str); 
like image 148
BoltClock Avatar answered Oct 23 '22 00:10

BoltClock


They are called backreferences and match grouped elements within the regexp.

If you surround a section of the regexp with brackets, then you can refer to it in the replace section (or indeed later in the same regexp, by the backreference which corresponds to its position.

Slash form, or dollar form can be used in replacements:

\1, \2 == $1, $2
like image 37
Orbling Avatar answered Oct 23 '22 02:10

Orbling