?: 正则表达式中的符号 [英] ?: Notation in Regular Expression

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

问题描述

对于我的一门课,我必须描述以下正则表达式:

for one of my classes I have to describe the following regular expression:

\b4[0-9]{12}(?:[0-9]{3})\b

据我所知,它选择的数字是:以 4 开头,后跟 12 位数字(每个数字在 0-9 之间),然后是另外 3 位数字.

I understand that it selects a number that: begins with 4, is followed by 12 digits (each between 0-9), and is followed by another 3 digits.

我不明白的是带分号 (?:....) 的问号.我尝试在网上查找以了解这意味着什么,但我发现的链接有些混乱;我希望有人可以让我快速了解这个示例中问号的作用.

What I don't understand is the the question mark with the semicolon (?:....). I've tried looking online to find out what this means but the links I've found were somewhat confusing; I was hoping someone could give me a quick basic idea of what the question mark does in this example.

推荐答案

这将是简短的答案.

当您使用 (?:) 时,这意味着 group 已匹配但未被捕获用于反向引用,即 非捕获团体.它不会存储在内存中供以后引用.

When you use (?:) it means that the group is matched but is not captured for back-referencing i.e non-capturing group. It's not stored in memory to be referenced later on.

例如:

(34)5\1

此正则表达式意味着您要查找 34 后跟 534.当然你可以把它写成 34534 但有时 captured group 是一个复杂的模式,你无法事先预测.

This regex means that you are looking for 34 followed by 5 and then again 34. Definitely you could write it as 34534 but sometimes the captured group is a complex pattern which you could not predict before hand.

所以任何与捕获组匹配的都应该再次出现.

So whatever is matched by capturing group should be appearing again.

用于反向引用的 Regex101 演示

替换时也使用反向引用.

Back-referencing is also used while replacement.

例如:

([A-Z]+)[0-9]+

此正则表达式将查找many 大写字母后跟many 数字.我希望用 found 大写字母替换整个模式.

This regex will look for many upper case letters followed by many digits. And I wish to replace this whole pattern just by found upper case letters.

然后我将使用 \1替换整个模式,它代表向后引用第一个捕获的组.

Then I would replace whole pattern by using \1 which stands for back-referencing first captured group.

Regex101 替换演示

如果您更改为 (?:[A-Z]+)[0-9]+ 这将不再捕获它,因此不能被引用回来.

If you change to (?:[A-Z]+)[0-9]+ this will no longer capture it and hence cannot be referenced back.

Regex101 非捕获组演示

实时答案.

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

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