Grouping and Backreferences
Syntax
Description
Example
(regex)
|
Round
brackets group the regex between them. They capture the text matched by
the regex inside them that can be reused in a backreference, and they
allow you to apply regex operators to the entire grouped regex. |
(abc){3}
matches abcabcabc
. First group matches abc
. |
(?:regex)
|
Non-capturing
parentheses group the regex so you can apply regex operators, but do
not capture anything and do not create backreferences. |
(?:abc){3}
matches abcabcabc
. No groups. |
\1
through \9
|
Substituted
with the text matched between the 1st through 9th pair of capturing
parentheses. Some regex flavors allow more than 9 backreferences. |
(abc|def)=\1
matches abc=abc
or def=def
, but not abc=def
or def=abc
. |
Modifiers
Syntax
Description
Example
(?i)
|
Turn
on case insensitivity for the remainder of the regular expression.
(Older regex flavors may turn it on for the entire regex.) |
te(?i)st
matches teST
but not TEST
. |
(?-i)
|
Turn off case insensitivity for the remainder of the regular expression. |
(?i)te(?-i)st
matches TEst
but not TEST
. |
(?s)
|
Turn
on "dot matches newline" for the remainder of the regular expression.
(Older regex flavors may turn it on for the entire regex.) |
|
(?-s)
|
Turn off "dot matches newline" for the remainder of the regular expression. |
|
(?m)
|
Caret
and dollar match after and before newlines for the remainder of the
regular expression. (Older regex flavors may apply this to the entire
regex.) |
|
(?-m)
|
Caret and dollar only match at the start and end of the string for the remainder of the regular expression. |
|
(?x)
|
Turn on free-spacing mode to ignore whitespace between regex tokens, and allow # comments. |
|
(?-x)
|
Turn off free-spacing mode. |
|
(?i-sm)
|
Turns
on the options "i" and "m", and turns off "s" for the remainder of the
regular expression. (Older regex flavors may apply this to the entire
regex.) |
|
(?i-sm:regex)
|
Matches the regex inside the span with the options "i" and "m" turned on, and "s" turned off. |
(?i:te)st
matches TEst
but not TEST
. |
Atomic Grouping and Possessive Quantifiers
Syntax
Description
Example
(?>regex)
|
Atomic
groups prevent the regex engine from backtracking back into the group
(forcing the group to discard part of its match) after a match has been
found for the group. Backtracking can occur inside the group before it
has matched completely, and the engine can backtrack past the entire
group, discarding its match entirely. Eliminating needless backtracking
provides a speed increase. Atomic grouping is often indispensable when
nesting quantifiers to prevent a catastrophic amount of backtracking as
the engine needlessly tries pointless permutations of the nested
quantifiers. |
x(?>\w+)x
is more efficient than x\w+x
if the second x cannot be matched. |
?+
, *+
, ++
and {m,n}+
|
Possessive
quantifiers are a limited yet syntactically cleaner alternative to
atomic grouping. Only available in a few regex flavors. They behave as
normal greedy quantifiers, except that they will not give up part of
their match for backtracking. |
x++
is identical to (?>x+)
|
Lookaround
Syntax
Description
Example
(?=regex)
|
Zero-width
positive lookahead. Matches at a position where the pattern inside the
lookahead can be matched. Matches only the position. It does not consume
any characters or expand the match. In a pattern like one(?=two)three
, both two
and three
have to match at the position where the match of one
ends. |
t(?=s)
matches the second t
in streets
. |
(?!regex)
|
Zero-width
negative lookahead. Identical to positive lookahead, except that the
overall match will only succeed if the regex inside the lookahead fails
to match. |
t(?!s)
matches the first t
in streets
. |
(?<=regex)
|
Zero-width
positive lookbehind. Matches at a position if the pattern inside the
lookahead can be matched ending at that position (i.e. to the left of
that position). Depending on the regex flavor you're using, you may not
be able to use quantifiers and/or alternation inside lookbehind. |
(?<=s)t
matches the first t
in streets
. |
(?<!regex)
|
Zero-width
negative lookbehind. Matches at a position if the pattern inside the
lookahead cannot be matched ending at that position. |
(?<!s)t
matches the second t
in streets
. |
Continuing from The Previous Match
Syntax
Description
Example
\G
|
Matches
at the position where the previous match ended, or the position where
the current match attempt started (depending on the tool or regex
flavor). Matches at the start of the string during the first match
attempt. |
\G[a-z]
first matches a
, then matches b
and then fails to match in ab_cd
. |
Conditionals
Syntax
Description
Example
(?(?=regex)then|else)
|
If
the lookahead succeeds, the "then" part must match for the overall
regex to match. If the lookahead fails, the "else" part must match for
the overall regex to match. Not just positive lookahead, but all four
lookarounds can be used. Note that the lookahead is zero-width, so the
"then" and "else" parts need to match and consume the part of the text
matched by the lookahead as well. |
(?(?<=a)b|c)
matches the second b
and the first c
in babxcac
|
(?(1)then|else)
|
If
the first capturing group took part in the match attempt thus far, the
"then" part must match for the overall regex to match. If the first
capturing group did not take part in the match, the "else" part must
match for the overall regex to match. |
(a)?(?(1)b|c)
matches ab
, the first c
and the second c
in babxcac
|
Comments
Syntax
Description
Example
(?#comment)
|
Everything between (?#
and )
is ignored by the regex engine. |
a(?#foobar)b
matches ab
|
相关推荐
正则表达式的基本语法,主要用于javascript开发时看正则表达式方便点
6正则表达式笔记[借鉴].pdf
Java正则表达式学习笔记,比较基础,适合初学者
正则表达式学习笔记正则表达式学习笔记正则表达式学习笔记正则表达式学习笔记正则表达式学习笔记正则表达式学习笔记
我写了很多的笔记,其中j2ee上册已经上传,现在这个正则表达式供大家学习。谢谢。
python正则表达式笔记
Python正则表达式笔记
正则表达式 txt文档 详细的总结正则表达式!
基于java的开发源码-java多线程反射泛型及正则表达式学习笔记和源码.zip 基于java的开发源码-java多线程反射泛型及正则表达式学习笔记和源码.zip 基于java的开发源码-java多线程反射泛型及正则表达式学习笔记和源码....
[小小明]Python正则表达式全套笔记v0.3(1.8万字干货)
正则表达式基础学习笔记!!!从基础开始正则表达式基础学习笔记!!!
js正则表达式笔记,可以运行,正则 笔记
Java正则表达式笔记.pdf
北风网正则表达式详细讲解学习笔记,讲解的比较基础适合初学者学习。
本文档是本人在学习正则表达式时所做笔记 介绍并解析了正则表达式 适合未学习过或者复习者使用 文末附加了常用的正则表达式表
php正则表达式笔记,本人手写的 //----正则匹配----- $p = '/example/' $str = "I am an example"; preg_match($p,$str); //如果$str中含有$p两个/中间的内容(内容不要求完全相同,要求为包含于关系),则返回真 //...
python正则表达式详解笔记
好东西才与大家分享!这是博客园一位老师的正则表达式教学笔记,很精炼的阐述,看了你一定不后悔……
关于正则表达式的定义,常用方法。看后使你更快了解如何使用正则表达式