(?:在正则表达式中做什么 [英] What does (?: do in a regular expression

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

问题描述

我遇到了一个我不完全理解的正则表达式-有人可以帮助我解密它吗?

I have come across a regular expression that I don't fully understand - can somebody help me in deciphering it:

^home(?:\/|\/index\.asp)?(?:\?.+)?$

它用于url匹配,上面的示例匹配以下url:

It is used in url matching and the above example matches the following urls:

home
home/
home/?a
home/?a=1
home/index.asp
home/index.asp?a
home/index.asp?a=1

在我看来,方括号(?:中的问号没有任何作用.有人可以启发我.

It seems to me that the question marks within the brackets (?: don't do anything. Can somebody enlighten me.

正在使用的regex版本是Classic ASP附带的版本,如果有帮助的话,可以在服务器上运行.

The version of regex being used is the one supplied with Classic ASP and is being run on the server if that helps at all.

推荐答案

(?:)创建一个非捕获组.它将所有内容组合在一起,而无需创建反向引用.

(?:) creates a non-capturing group. It groups things together without creating a backreference.

后向引用是您可以在表达式中引用的部分,也可以是可能的替换部分(通常通过说\1$1等-取决于风味).通常,在以编程语言使用正则表达式之后,通常也可以从匹配项中提取它们.使用(?:)的唯一原因是避免创建新的反向引用,从而避免增加组号并节省(通常可以忽略不计的)内存

A backreference is a part you can refer to in the expression or a possible replacement (usually by saying \1 or $1 etc - depending on flavor). You can also usually extract them from a match afterwards when using regex in a programming language. The only reason for using (?:) is to avoid creating a new backreference, which avoids incrementing the group number, and saves (a usually negligible amount of) memory

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

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