RegularExpression

Python Regular Expression[re]

EN version

SyntaxDescriptionCharacterExpression ExampleFull Match Example 
General Characters     
Match itselfMatches the character itselfabcabcabc 
.Matches any character except a newline \n. In DOTALL mode, it can also match newlines.a.ca.cabc 
\Escape character, makes the following character literal. Use \ to match special characters.a\.ca\.ca.c 
[ ... ]Character set (character class). Matches any character in the set. Can specify characters or ranges.a[bcd]ea[bcd]eabe, ace, ade 
 Special characters lose their meaning in character sets. Escape -, ], ^ if needed.[a-c][^abc]Any character except abc 
Predefined Character Sets (can also be used within [ ... ])     
\dMatches a digit [0-9]a\dca\dca1c, a3c 
\DMatches a non-digit [^0-9]a\Dca\Dcabc, axc 
\sMatches a whitespace character [ \t\r\n\f\v]a\sca\sca c, a\tc 
\SMatches a non-whitespace character [^ \t\r\n\f\v]a\Sca\Scabc, axc 
\wMatches a word character [A-Za-z0-9_]a\wca\wcabc, a1c 
\WMatches a non-word character [^A-Za-z0-9_]a\Wca\Wca@c 
Quantifiers (used after a character or ( ... ))     
*Matches the preceding character 0 or more timesabc*abc*ab, abc, abcc 
+Matches the preceding character 1 or more timesabc+abc+abc, abcc 
?Matches the preceding character 0 or 1 timeabc?abc?ab, abc 
{m}Matches the preceding character exactly m timesab{2}cab{2}cabbc 
{m,n}Matches the preceding character between m and n times, inclusiveab{1,2}cab{1,2}cabc, abbc 
*? +? ??Makes * + ? {m,n} non-greedya.*?See examples belowNon-greedy match 
Boundary Matchers (do not consume characters in the string)     
^Matches the beginning of the string. In multiline mode, matches the start of each line.^abc^abcabc 
$Matches the end of the string. In multiline mode, matches the end of each line.abc$abc$abc 
\AMatches only at the start of the string\Aabc\Aabcabc 
\ZMatches only at the end of the stringabc\Zabc\Zabc 
\bMatches a word boundary\babc\b\babc\babc 
\BMatches a non-word boundarya\Bbca\Bbca@bc 
Logical and Grouping     
``Matches either the expression on the left or right. Left side is tried first.abc | defabc|defabc, def
( ... )Groups expressions and assigns a number from 1 onwards for backreferences(abc){2}(abc){2}abcabc 
(?P<name> ... )Named group, provides an additional name for backreferences    
\<number>Refers to the group with the given number in backreference(\d)abc\1(\d)abc\11abc1 
(?P=name)Refers to the named group in backreference    
Special Constructs (not counted as a group)     
(?: ... )Non-capturing group    
(?iLmsux)Mode modifiers to alter matching behavior, can specify multiple(?i)abc(?i)abcAbc, ABC 
(?# ... )Inline comment, ignored by the regex engineabc(?#comment)123abc(?#comment)123abc123 
(?= ... )Positive lookahead, does not consume charactersa(?=\d)a(?=\d)a1, a2 
(?! ... )Negative lookahead, does not consume charactersa(?!\d)a(?!\d)a, ab 
(?<= ... )Positive lookbehind(?<=\d)a(?<=\d)a1a, 2a 
(?<! ... )Negative lookbehind(?<!\d)a(?<!\d)aa 
(?(id/name)yes-pattern | no-pattern)Conditional matching, matches yes-pattern if the group matches, else no-pattern(\d)abc(?(1)\d | abc)1abc1, abcabc  

CN version

语法说明字符表达式实例完整匹配的字符串 
一般字符     
匹配自身匹配自身字符abcabcabc 
.匹配任意除换行符\n外的字符。在DOTALL模式中也能匹配换行符。a.ca.cabc 
\转义字符,使后一个字符恢复原意。如字符串中有特殊字符要匹配时,可用 \ 转义a\.ca\.ca.c 
[ ... ]字符集(字符类),对应位置可以是字符集中任意字符。字符集可以列出字符或用范围表示。a[bcd]ea[bcd]eabe, ace, ade 
 特殊字符在字符集中失去原有含义。字符集中如需 -]^,需转义或调整位置。[a-c][^abc]abc以外的字符 
预定义字符集 (可以写在字符集[ ... ]中)     
\d匹配数字 [0-9]a\dca\dca1c, a3c 
\D匹配非数字 [^0-9]a\Dca\Dcabc, axc 
\s匹配空白字符 [ \t\r\n\f\v]a\sca\sca c, a\tc 
\S匹配非空白字符 [^ \t\r\n\f\v]a\Sca\Scabc, axc 
\w匹配单词字符 [A-Za-z0-9_]a\wca\wcabc, a1c 
\W匹配非单词字符 [^A-Za-z0-9_]a\Wca\Wca@c 
数量词 (用在字符或(...)之后)     
*匹配前一个字符 0 次或多次abc*abc*ab, abc, abcc 
+匹配前一个字符 1 次或多次abc+abc+abc, abcc 
?匹配前一个字符 0 次或 1 次abc?abc?ab, abc 
{m}匹配前一个字符 m 次ab{2}cab{2}cabbc 
{m,n}匹配前一个字符 m 至 n 次,m 或 n 可省略ab{1,2}cab{1,2}cabc, abbc 
*? +? ??使 * + ? {m,n} 变成非贪婪模式a.*?示例见下文非贪婪匹配 
边界匹配 (不消耗待匹配字符串中的字符)     
^匹配字符串开头,在多行模式中匹配每一行的开头^abc^abcabc 
$匹配字符串末尾,在多行模式中匹配每一行的末尾abc$abc$abc 
\A仅匹配字符串开头\Aabc\Aabcabc 
\Z仅匹配字符串末尾abc\Zabc\Zabc 
\b匹配单词边界\babc\b\babc\babc 
\B匹配非单词边界a\Bbca\Bbca@bc 
逻辑、分组     
``代表左右表达式任意匹配一个,左表达式优先abc | defabc|defabc, def
( ... )分组,编号从1开始,可使用编号引用(abc){2}(abc){2}abcabc 
(?P<name> ...)分组,除编号外再指定别名    
\<number>引用编号为的分组匹配到的字符串(\d)abc\1(\d)abc\11abc1 
(?P=name)引用别名为的分组匹配到的字符串    
特殊构造 (不作为分组)     
(?: ...)非捕获性分组    
(?iLmsux)模式修饰符,用于调整匹配模式(?i)abc(?i)abcAbc, ABC 
(?# ...)注释,# 后的内容会被忽略abc(?# comment)123abc123abc123 
(?= ...)正向预查,不消耗字符串内容a(?=\d)a(?=\d)a1, a2 
(?! ...)负向预查,不消耗字符串内容a(?!\d)a(?!\d)a, ab 
(?<= ...)正向后发断言(?<=\d)a(?<=\d)a1a, 2a 
(?<! ...)负向后发断言(?<!\d)a(?<!\d)aa 
(?(id/name)yes-pattern | no-pattern)条件判断,若编号或别名组匹配到字符则匹配 yes-pattern ,否则 no-pattern(\d)abc(?(1)\d | abc)1abc1, abcabc  

Back to top

Engineering & Philosophy & Life Experience - A Motorcycle rider and loving husband.