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

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

问题描述

我遇到了一个我不完全理解的正则表达式 - 谁能帮我破译它:

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.

正在使用的正则表达式版本是 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天全站免登陆