带问号的正则表达式 [英] Regular expressions with question mark

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

问题描述

colour?r 匹配 colorcolour

但是下面的 ? 是做什么的?

but what does the following ? do?

(first group)(second group)?(third group)

推荐答案

它是一个量词,表示零或一".它适用于它前面的原子,因此,这里是 u.

It is a quantifier which means "zero or one". It applies to the atom preceding it, therefore, here, the u.

它是众多正则表达式元字符之一,这意味着,如果您想匹配文字问号,则必须对其进行转义(如 \?),除了 在字符类中.

It is one of the numerous regex metacharacters, which means, if you want to match a literal question mark, you'll have to escape it (as in \?), except in a character class.

至于你的第二个例子:

(first group)(second group)?(third group)

意思完全一样.分组操作符 (...) 除了允许反向引用外,还有一个很好的效果,可以将任何东西变成括号变成原子.

it has exactly the same meaning. The grouping operator, (...), apart from allowing backreferences, also has the nice effect to turn anything into parens into an atom.

也就是说,这里正则表达式second group匹配的文本可以出现0次或1次,类似于Colour?r中的u代码>.

Which means, here, the text matched by the regex second group can appear 0 or 1 time, similarly to the u in Colou?r.

http://www.regular-expressions.info/reference.html

这篇关于带问号的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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