This question already has an answer here:
这个问题已经有了答案:
- What is a non-capturing group? What does (?:) do? 14 answers
- 什么是非捕获群?(?)做什么?14个答案
I have the following Java regex, which I didn't write and I am trying to modify:
我有下面的Java regex,我没有写,我正在修改:
^class-map(?:(\\s+match-all)|(\\s+match-any))?(\\s+[\\x21-\\x7e]{1,40})$
^ ^
It's similar to this one.
它和这个很相似。
Note the first question mark. Does it mean that the group is optional? There is already a question mark after the corresponding )
. Does the colon have a special meaning in regex?
注意第一个问号。这是否意味着组是可选的?在对应的后面已经有一个问号)。在regex中冒号有特殊的含义吗?
The regex compiles fine, and there are already JUnit tests that show how it works. It's just that I'm a bit confused about why the first question mark and colon are there.
regex编译良好,并且已经有JUnit测试显示了它是如何工作的。只是我有点搞不懂为什么第一个问号和冒号在这里。
2 个解决方案
#1
111
(?:
starts a non-capturing group. It's no different to (
unless you're retrieving groups from the regex after use. See What is a non-capturing group? What does a question mark followed by a colon (?:) mean?.
(?:启动一个非捕获组。它与(除非您在使用后从regex中检索组。看到什么是非捕获群了吗?问号后面跟冒号是什么意思?
#2
30
A little late to this thread - just to build on ryanp's answer.
这条线有点晚了——只是基于ryanp的答案。
Assuming you have the string aaabbbccc
假设你有一个字符串aaabbbccc
Regular Expression
(a)+(b)+(c)+
This would give you the following 3 groups that matched:
这将给你以下3组匹配:
['a', 'b', 'c']
Regular Expression with non-capturing parenthesis
Use the ?:
in the first group
在第一组中使用?:
(?:a)+(b)+(c)+
and you would get the following groups that matched:
你会得到以下匹配的组:
['b', 'c']
Hence why it is called "non-capturing parenthesis"
因此它被称为“非捕获括号”
Example use case:
Sometime you use parenthesis for other things. For example to set the bounds of the |
or operator:
有时你用括号来表示其他东西。例如设置|或算子的界:
"New (York|Jersey)"
In this case, you are only using the parenthesis for the or |
switch, and you don't really want to capture this data. Use the non-capturing parenthesis to indicate that:
在这种情况下,您只使用or |开关的括号,而不希望捕获此数据。使用非捕获括号表示:
"New (?:York|Jersey)"
#1
111
(?:
starts a non-capturing group. It's no different to (
unless you're retrieving groups from the regex after use. See What is a non-capturing group? What does a question mark followed by a colon (?:) mean?.
(?:启动一个非捕获组。它与(除非您在使用后从regex中检索组。看到什么是非捕获群了吗?问号后面跟冒号是什么意思?
#2
30
A little late to this thread - just to build on ryanp's answer.
这条线有点晚了——只是基于ryanp的答案。
Assuming you have the string aaabbbccc
假设你有一个字符串aaabbbccc
Regular Expression
(a)+(b)+(c)+
This would give you the following 3 groups that matched:
这将给你以下3组匹配:
['a', 'b', 'c']
Regular Expression with non-capturing parenthesis
Use the ?:
in the first group
在第一组中使用?:
(?:a)+(b)+(c)+
and you would get the following groups that matched:
你会得到以下匹配的组:
['b', 'c']
Hence why it is called "non-capturing parenthesis"
因此它被称为“非捕获括号”
Example use case:
Sometime you use parenthesis for other things. For example to set the bounds of the |
or operator:
有时你用括号来表示其他东西。例如设置|或算子的界:
"New (York|Jersey)"
In this case, you are only using the parenthesis for the or |
switch, and you don't really want to capture this data. Use the non-capturing parenthesis to indicate that:
在这种情况下,您只使用or |开关的括号,而不希望捕获此数据。使用非捕获括号表示:
"New (?:York|Jersey)"