Ruby regex what does the \1 mean for gsub Ruby regex what does the \1 mean for gsub ruby ruby

Ruby regex what does the \1 mean for gsub


Each item that you surround with parenthesis in the searching part will correspond to a number \1, \2, etc., in the substitution part.

In your example, there's only one item surrounded by parenthesis, the "(bar)" item, so anywhere you put a \1 is where the part inside the parenthesis, will be swapped in. You can put in the \1 multiple times, which is handy if you want to repeat that found item, so you could legitimately write car\1\1\1 and "bar" will be swapped in three times.

There's no use for \2 because there's only one item surrounded by parentheses. However, if you had (bar)(jar), then the \1 would represent "bar" and \2 would represent "jar".

You could even do things like this:

\1\2\1\2\2\1

which would become:

barjarbarjarjarbar

Here's a real-world example where this comes in handy. Let's say you have a name list like this:

Jones, Tom  Smith, Alan  Smith, Dave  Wilson, Bud

and you want to change it to this:

Tom Jones  Alan Smith  Dave Smith  Bud Wilson

You could search for:

(.+), (.+)

and replace with:

\2 \1

You could also replace with:

\1: \2 \1  

Which would become:

Jones: Tom Jones  Smith: Alan Smith  Smith: Dave Smith  Wilson: Bud Wilson


Generally speaking \N is replaced with the N-th group specified in the regular expression. The first matched group is referenced by \1 and the maximum number of groups is 9.

Some examples:

# wrap every integer into brackets'1 2 34'.gsub(/(\d+)/, '[\1]')# => "[1] [2] [34]"# gsub with two groups: swap couples of integers'<1,2> <3,4>'.gsub(/(\d+),(\d+)/, '\2,\1')# => "<2,1> <4,3>" # you can reference the same group more than once'1 2 34'.gsub(/(\d+)/, '<\1,\1>')#  => "<1,1> <2,2> <34,34>"# a slightly more complex example'Jim Morrison'.sub(/([A-Z])[a-z]+ ([A-Z][a-z]+)/, '\2 \1.')# => "Morrison J."


The \1 is syntax for the last capture in a regular expression using the () like you said. It says whatever was matched, replace it with that.

You can continually use () groups and their respective \2 to continue to replace what you matched.