Ruby regex what does the \1 mean for gsub

蹲街弑〆低调 提交于 2019-12-20 09:19:03

问题


What does the \1 do?

For example

"foo bar bag".gsub(/(bar)/,'car\1')

I believe it has something to do with how you use parentheses, but I'm not really sure. Could someone explain it to me? And can you do stuff like \2? If so, what would that do?


回答1:


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



回答2:


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."



回答3:


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.



来源:https://stackoverflow.com/questions/15825872/ruby-regex-what-does-the-1-mean-for-gsub

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!