正则表达式:问号和冒号 [英] Regex: Question mark and colon

查看:335
本文介绍了正则表达式:问号和冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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?

正则表达式编译正常,并且已经有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.

推荐答案

(?:启动非捕获组。与)没有区别,除非您在使用后从正则表达式中检索组。请参阅< a href =https://stackoverflow.com/q/3512471/1143427>什么是非捕获组?问号后面跟冒号(?:)是什么意思?。

(?: 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆