正则表达式:?:表示法(问号和冒号表示法) [英] Regex: ?: notation (Question mark and colon notation)
问题描述
我有以下Java正则表达式,我没有写,并且正在尝试修改:
I have the following Java regex, which I didn't write and I am trying to modify:
^class-map(?:(\\s+match-all)|(\\s+match-any))?(\\s+[\\x21-\\x7e]{1,40})$
^ ^
类似于这一个.
请注意第一个问号.这是否意味着该组是可选的?相应的)
后面已经有一个问号.冒号在正则表达式中有特殊含义吗?
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可以很好地编译,并且已经有JUnit测试显示它如何工作.只是我对为什么第一个问号和冒号存在感到困惑.
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.
推荐答案
(?:
启动一个非捕获组.除非您在使用后从正则表达式中检索组,否则它与(
没有什么不同.请参阅什么是非捕获组? .
(?:
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?.
这篇关于正则表达式:?:表示法(问号和冒号表示法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!