`?` 在这个 Perl 正则表达式中是什么意思? [英] What does `?` mean in this Perl regex?
问题描述
我有一个 Perl 正则表达式.但我不确定是什么?"在这种情况下意味着.
I have a Perl regex. But I'm not sure what "?" means in this context.
m#(?:\w+)#
?
在这里是什么意思?
推荐答案
在这种情况下,?
实际上是与 :
结合使用的.放在一起, ?:
在分组的开头意味着分组但不捕获括号内的文本/模式(例如,它不会存储在任何反向引用中,例如 \1
或 $1
,因此您将无法直接访问分组文本).
In this case, the ?
is actually being used in connection with the :
. Put together, ?:
at the beginning of a grouping means to group but not capture the text/pattern within the parentheses (as in, it will not be stored in any backreferences like \1
or $1
, so you will not be able to access the grouped text directly).
更具体地说,?
在正则表达式中具有三个不同的含义:
More specifically, a ?
has three distinct meanings in regex:
?
量词表示表达式的零次或一次重复".我见过的规范示例之一是s?he
它将匹配she
和he
因为?
> 使s
成为可选"
The
?
quantifier signifies "zero or one repetitions" of an expression. One of the canonical examples I've seen iss?he
which will match bothshe
andhe
since the?
makes thes
"optional"
当一个量词(+
、*
、?
,或者一般的{n,m}
code>) 后跟一个 ?
那么匹配是非贪婪的(即它将匹配从允许匹配继续的那个位置开始的最短字符串)
When a quantifier (+
, *
, ?
, or the general {n,m}
) is followed by a ?
then the match is non-greedy (i.e. it will match the shortest string starting from that position that allows the match to proceed)
带括号的组开头的 ?
表示您要执行特殊操作.在这种情况下,:
表示分组但不捕获.可用操作的确切列表会因正则表达式引擎而有所不同,但这里是其中一些的列表(不一定包含所有内容):
A ?
at the beginning of a parenthesized group signifies that you want to perform a special action. As in this case, :
means to group but not capture. The exact list of actions available will vary somewhat from one regex engine to another, but here's a list (not necessarily all-inclusive) of some of them:
A.非捕获组:(?:text)
B. 环视:<代码>(?= A)代码>对于一个先行, ?!
用于负向前瞻,或 ?<=
和 ?<!
用于后向(分别为正和负).
C. 条件匹配:(?(condition)then|else)代码>.
D. 原子分组:a(?>bc|b)c
(匹配 abcc
但不 abc
;见链接)
E. 内联启用/禁用正则表达式匹配修饰符:?i
启用模式,?-i
禁用.您还可以通过简单地连接它们一次启用/禁用多个修饰符,例如 ?im
(i
不区分大小写,m
是多行的).
F. 命名捕获组:(?P
,稍后可以使用 (?P=name)
引用..NET 正则表达式引擎使用语法 (?
代替.
G. 评论:(?#Comment text)
.我个人认为这只会增加混乱,但我想它可以起到一些作用...自由间距模式 可能是更好的选择((?x)
修饰符).
A. Non-capturing group: (?:text)
B. Lookaround: (?=a)
for a lookahead, ?!
for negative lookahead, or ?<=
and ?<!
for lookbehinds (positive and negative, respectively).
C. Conditional Matches: (?(condition)then|else)
.
D. Atomic Grouping: a(?>bc|b)c
(matches abcc
but not abc
; see the link)
E. Inline enabling/disabling of regex matching modifiers: ?i
to enable a mode, ?-i
to disable. You can also enable/disable more than one modifier at a time by simply concatenating them, such as ?im
(i
is case insensitive and m
is multiline).
F. Named capture groups: (?P<name>pattern)
, which can later be referenced using (?P=name)
. The .NET regex engine uses the syntax (?<name>pattern)
instead.
G. Comments: (?#Comment text)
. I personally think this just adds clutter, but I guess it could serve some use...free-spacing mode might be a better option (the (?x)
modifier).
所以基本上,在 代码>的目的仅仅是上下文.如果您想要零次或多次重复文字
(
字符,您必须使用 \(?
来转义括号.
So essentially, the purpose of the ?
is just contextual. If you wanted zero or more repetitions of a literal (
character you'd have to use \(?
to escape the paren.
这篇关于`?` 在这个 Perl 正则表达式中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!