【Python 正则表达式】repetition qualifiers
Repetition qualifiers (*
, +
, ?
, {m,n}
, etc) cannot be directly nested. This avoids ambiguity with the non-greedy modifier suffix ?
, and with other modifiers in other implementations. To apply a second repetition to an inner repetition, parentheses may be used. For example, the expression (?:a{6})*
matches any multiple of six 'a'
characters.
.
-
(Dot.) In the default mode, this matches any character except a newline. If the
DOTALL
flag has been specified, this matches any character including a newline.
^
-
(Caret.) Matches the start of the string, and in
MULTILINE
mode also matches immediately after each newline-
Special characters lose their special meaning inside sets. For example,
[(+*)]
will match any of the literal characters'('
,'+'
,'*'
, or')'
. [^^]
will match any character except'^'
.^
has no special meaning if it’s not the first character in the set.- To match a literal
']'
inside a set, precede it with a backslash, or place it at the beginning of the set. For example, both[()[\]{}]
and[]()[{}]
will both match a parenthesis. - This means that once A matches, B will not be tested further, even if it would produce a longer overall match. In other words, the
'|'
operator is never greedy. (...)
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group;
(?aiLmsux)
(One or more letters from the set
'a'
,'i'
,'L'
,'m'
,'s'
,'u'
,'x'
.) The group matches the empty string; the letters set the corresponding flags:re.A
(ASCII-only matching),re.I
(ignore case),re.L
(locale dependent),re.M
(multi-line),re.S
(dot matches all),re.U
(Unicode matching), andre.X
(verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to there.compile()
function. Flags should be used first in the expression string.
(?P<name>...)
(?P<quote>['"].*?(?P=quote) (?P<quote>['"].*?\1
(?P=name)
-
A backreference to a named group; it matches whatever text was matched by the earlier group named name.
(?#...)
-
A comment; the contents of the parentheses are simply ignored.
(?!...)
Matches if
...
doesn’t match next. This is a negative lookahead assertion. For example,Isaac (?!Asimov)
will match'Isaac '
only if it’s not followed by'Asimov'
.
-
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)