阅读背景:

正则表达式中的反斜杠后面的数字是什么意思?

来源:互联网 
(a|b)
(a|b)\1

What does \1 mean in this expression?

在这个表达式中,\1是什么意思?

2 个解决方案

#1


39  

\1 - it means the first capturing group in the matched expression. \n would be the nth capturing group. (Note that \0 would be whole match). In many engines, the upperlimit for n is 9, but some support up to 99 as well.

\1 -它表示匹配表达式中的第一个捕获组。n将是第n个捕获组。(注意,\0将是完整的匹配)。在许多引擎中,n的上限是9,但也有一些支持高达99。

When used in regex like (a|b)\1, it means that after a or b, the next character should be the first captured group, which is a or b so the regex here would match aa or bb.

当在regex中使用(a|b)\1时,它意味着在a或b之后,下一个字符应该是第一个捕获的组,即a或b,因此这里的regex将匹配aa或bb。

#2


12  

If refers to what was matched in the first set of parentheses, the first group. Subsequent number means subsequent parentheses.

如果指的是第一个括号集合中匹配的对象,即第一组。后面的数字表示后面的括号。

(1|2)(3|4)\1\2

Would match:

将匹配:

1313
1414
2323
2424

Not that if you have nested groups, just count from the opening brace (left brace).

如果你有嵌套的组,那就从左括号开始计算。

(groupOne(groupTwo)stillOne(groupThree(groupFour)))

What does

(a|b)\1

What does \1 mean in this expression?

在这个表达式中,\1是什么意思?

2 个解决方案

#1


39  

\1 - it means the first capturing group in the matched expression. \n would be the nth capturing group. (Note that \0 would be whole match). In many engines, the upperlimit for n is 9, but some support up to 99 as well.

\1 -它表示匹配表达式中的第一个捕获组。n将是第n个捕获组。(注意,\0将是完整的匹配)。在许多引擎中,n的上限是9,但也有一些支持高达99。

When used in regex like (a|b)\1, it means that after a or b, the next character should be the first captured group, which is a or b so the regex here would match aa or bb.

当在regex中使用(a|b)\1时,它意味着在a或b之后,下一个字符应该是第一个捕获的组,即a或b,因此这里的regex将匹配aa或bb。

#2


12  

If refers to what was matched in the first set of parentheses, the first group. Subsequent number means subsequent parentheses.

如果指的是第一个括号集合中匹配的对象,即第一组。后面的数字表示后面的括号。

(1|2)(3|4)\1\2

Would match:

将匹配:

1313
1414
2323
2424

Not that if you have nested groups, just count from the opening brace (left brace).

如果你有嵌套的组,那就从左括号开始计算。

(groupOne(groupTwo)stillOne(groupThree(groupFour)))

mean in this expression? mean in this expression




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: