What is this?
This is a collection of common Q&A. This is also a Community Wiki, so everyone is invited to participate in maintaining it.
这是一组常见的问答。这也是一个社区维基,所以每个人都被邀请来维护它。
Why is this?
regex is suffering from give me ze code
type of questions and poor answers with no explanation. This reference is meant to provide links to quality Q&A.
regex正在遭受这样的问题:给我ze代码类型的问题和糟糕的答案,没有任何解释。本参考资料旨在提供质量问答的链接。
What's the scope?
This reference is meant for the following languages: php, perl, javascript, python, ruby, java, .net.
此引用用于以下语言:php、perl、javascript、python、ruby、java、.net。
This might be too broad, but these languages share the same syntax. For specific features there's the tag of the language behind it, example:
这可能太宽泛了,但是这些语言具有相同的语法。对于特定的特性,它后面有语言的标记,例如:
- What are regular expression Balancing Groups? .net
- 什么是正则表达式平衡组?net
1 个解决方案
#1
672
The Stack Overflow Regular Expressions FAQ
Online tutorials
在线教程
- RegexOne
- RegexOne
- Regular Expressions Info
- 正则表达式的信息
Quantifiers
量词
- Zero-or-more:
*
:greedy,*?
:reluctant,*+
:possessive - 零个或多个:*:贪婪、* ?:* +:占有欲很强,不情愿
- One-or-more:
+
:greedy,+?
:reluctant,++
:possessive - 一个或多个:+:贪婪,+ ?:不情愿,+ +:所有格
?
:optional (zero-or-one)- :可选(0或1)
- Min/max ranges (all inclusive):
{n,m}
:between n & m,{n,}
:n-or-more,{n}
:exactly n - Min/max ranges (all): {n,m}:between n & m, {n,}:n-or-more, {n}:exactly n
- Differences between greedy, reluctant (a.k.a. "lazy", "ungreedy") and possessive quantifier:
- Greedy vs. Reluctant vs. Possessive Quantifiers
- 贪婪,不情愿,占有,量词
- In-depth discussion on the differences between greedy versus non-greedy
- 深入探讨了贪婪与非贪婪的区别
- What's the difference between
{n}
and{n}?
- {n}和{n}之间有什么区别?
- Can someone explain Possessive Quantifiers to me? php, perl, java, ruby
- 有人能给我解释一下所有格的量词吗?php、perl、java、ruby
- Emulating possessive quantifiers .net
- 模拟所有格量词。net
- Non-Stack Overflow references: From Oracle, regular-expressions.info
- 非堆栈溢出引用:来自Oracle,正则表达式。info
- 贪婪、不情愿之间的区别。“懒”、“不贪心”和所有格量词:贪婪与不情愿与占有量词深入探讨贪婪与非贪婪的区别{n}与{n}的区别是什么?有人能给我解释一下所有格的量词吗?php, perl, java, ruby仿真所有格。net非堆栈溢出引用:来自Oracle,正则表达式。info。
Character Classes
字符类
- What is the difference between square brackets and parentheses?
- 方括号和圆括号的区别是什么?
-
[...]
: any one character,[^...]
: negated/any character but - […:任何一个字符(^……]:否定/任何字符
-
[^]
matches any one character including newlines javascript - (^)匹配任何一个字符包括换行javascript
-
[\w-[\d]]
/[a-z-[qz]]
: set subtraction .net, xml-schema, xpath, JGSoft - [\w-[\d]] / [a-z-[qz]:设置减法。net、xml-schema、xpath、JGSoft
-
[\w&&[^\d]]
: set intersection java, ruby 1.9+ - (\ w ^ \[d]]:设置交叉java,ruby 1.9 +
-
[[:alpha:]]
:POSIX character classes - [[:α:]]:POSIX字符类
-
Why do
[^\\D2]
,[^[^0-9]2]
,[^2[^0-9]]
get different results in Java? java - 为什么[^ \ \ D2]、[^ ^ 0 - 9][2],[^ 2[^ 0 - 9]]在Java中得到不同的结果?java
- Shorthand:
- Digit:
\d
:digit,\D
:non-digit - 数字:\ d:\ d:non-digit数字
- Word character (Letter, digit, underscore):
\w
:word character,\W
:non-word character - 单词字符(字母、数字、下划线):只\w:单词字符,只\w:非单词字符
- Whitespace:
\s
:whitespace,\S
:non-whitespace - 空白:\ s:空格、\ s:非空
- Digit:
- 速记:只\d:只\d:只\d:非位字字符(字母、数字、下划线):只\w:字字符,只\w:非字字符空格:只\s:空格,只\s:非空格
- Unicode categories (
\p{L}, \P{L}
, etc.) - Unicode类别(\p{L}, \p{L},等等)
Escape Sequences
转义序列
- Horizontal whitespace:
\h
:space-or-tab,\t
:tab - 水平空白:\ h:space-or-tab \ t:选项卡
- Newlines:
\r
,\n
:carriage return and line feed- \r \n:回车和换行
-
\R
:generic newline php - \ R:通用php换行符
- 换行:\r, \n:回车和换行:通用换行php
- Negated whitespace sequences:
\H
:Non horizontal whitespace character,\V
:Non vertical whitespace character,\N
:Non line feed character pcre php5 java-8 - 负空格序列:\H:非水平空格字符,\V:非垂直空格字符,\N:非换行字符pcre php5 java-8
- Other:
\v
:vertical tab,\e
:the escape character - 其他:\v:竖屏,\e:转义字符
Anchors
锚
-
^
:start of line/input,\b
:word boundary, and\B
:non-word boundary,$
:end of line/input - ^:开始的行/输入,\ b:单词边界,\ b:非单词边界,美元:行结束/输入
-
\A
:start of input,\Z
:end of input php, perl, ruby - \A:开始输入,\Z:结束输入php, perl, ruby
-
\z
:the very end of input (\Z
in Python) .net, php, pcre, java, ruby, icu, swift, objective-c - net, php, pcre, java, ruby, icu, swift, objective-c
-
\G
:start of match php, perl, ruby - \G:开始匹配php、perl、ruby
(Also see "Flavor-Specific Information → Java → The functions in Matcher
")
(也看到“Flavor-Specific信息→Java→匹配器”功能)
Groups
组
-
(...)
:capture group,(?:)
:non-capture group- Why is my repeating capturing group only capturing the last match?
- 为什么我的重复捕获组只捕获最后一个匹配?
- (…):捕获组,(?:):非捕获组为什么我的重复捕获组只捕获最后一个匹配?
-
\1
:backreference and capture-group reference,$1
:capture group reference- What's the meaning of a number after a backslash in a regular expression?
- 正则表达式中的反斜杠后面的数字是什么意思?
-
\g<1>123
:How to follow a numbered capture group, such as\1
, with a number?: python - \g<1>123:如何跟随编号捕获组,例如\1,使用编号?:python
- \1:反向引用和捕获组引用,$1:捕获组引用正则表达式中反斜杠后的数字的含义是什么?\g<1>123:如何跟随编号捕获组,例如\1,使用编号?:python
- What does a subpattern
(?i:regex)
mean? - 子模式(?i:regex)是什么意思?
- What does the 'P' in
(?P<group_name>regexp)
mean? -
P (?P
regexp)中的“P”是什么意思? -
(?>)
:atomic group or independent group,(?|)
:branch reset- Equivalent of branch reset in .NET/C# .net
- 相当于在.net / c# .net中重置分支
- (?>):原子组或独立组(?|):在.net / c# .net中,分支复位等价于分支复位
- Named capture groups:
- General named capturing group reference at
regular-expressions.info
- 一般命名为捕获组参考在常规表达。info
-
java:
(?<groupname>regex)
: Overview and naming rules (Non-Stack Overflow links) -
java: (?
regex):概述和命名规则(非堆栈溢出链接) - Other languages:
(?P<groupname>regex)
python,(?<groupname>regex)
.net,(?<groupname>regex)
perl,(?P<groupname>regex)
and(?<groupname>regex)
php -
其他语言:(?P
regex) python、(? regex) .net、(? regex) perl、(?P regex)和(? regex) php
- General named capturing group reference at
-
命名的捕获组:一般命名的捕获组引用在正则表达式。info java: (?
regex):概述和命名规则(非堆栈溢出链接)其他语言:(?P regex) python, (? 正则表达式). perl, (? 正则表达式),(? regex)和(? regex) php。
Lookarounds
看看
- Lookaheads:
(?=...)
:positive,(?!...)
:negative - 超前:(? =…):积极的,(? !…):消极
- Lookbehinds:
(?<=...)
:positive,(?<!...)
:negative (not supported by javascript) - (?<=…):正数(?
- Lookbehind limits in:
- Lookbehinds need to be constant-length php, perl, python, ruby
- 后面的代码必须是常量长度的php、perl、python、ruby
-
Lookarounds of limited length
{0,n}
java - 有限长度{0,n} java的查找程序
- Variable length lookbehinds are allowed .net
- 可变长度的后视镜是允许的。net
- 在:Lookbehind限值必须是恒长php、perl、python、ruby等长度有限的{0,n} java可变长度的Lookbehind值是允许的。net
- Lookbehind alternatives:
-
Using
\K
php, perl (Flavors that support\K
) - 使用\K php、perl(支持\K)
-
Alternative regex module for Python python
- The hacky way
- 出租汽车司机的方式
- JavaScript negative lookbehind equivalents External link
- JavaScript负的查找隐藏对等的外部链接
- Python的另一个regex模块,JavaScript负查找隐藏等效的外部链接
-
Using
- 查找替代方案:使用\K php、perl(支持\K) Python的可选regex模块,使用hacky方式JavaScript负查找等效外部链接
Modifiers
修饰符
- Most flavors:
g
:global,i
:case-insensitive,u
:unicode,x
:whitespace-extended - 大多数口味:g:global, i:大小写不敏感,u:unicode, x:whitespac -extended
-
c
:current position perle
:expression php perlo
:once ruby - c:当前位置perl e:expression php perl o:once ruby
-
m
:multiline php perl python javascript .net java,m
:(non)multiline ruby - m:多行php perl python javascript。net java, m:(非)多行ruby
-
s
:single line (not supported by javascript or ruby),s
workaround javascript - s:单行(不支持javascript或ruby), javascript的解决方案
-
S
:study phpU
:ungreedy php r - 学习php U:不贪心的php r
- How to convert preg_replace e to preg_replace_callback?
- 如何将preg_replace e转换为preg_replace_callback?
- What are inline modifiers?
- 内联修饰词是什么?
- What is '?-mix' in a Ruby Regular Expression
- “是什么?-在Ruby正则表达式中混合
Other:
其他:
-
|
:alternation (OR) operator,.
:any character,[.]
:literal dot character - 变换(或)运算符:任意字符,[。]:文字点性格
- What special characters must be escaped?
- 需要转义哪些特殊字符?
- Control verbs (php and perl):
(*PRUNE)
,(*SKIP)
,(*FAIL)
and(*F)
-
php only:
(*BSR_ANYCRLF)
- php只:(* BSR_ANYCRLF)
-
php only:
- 控制动词(php和perl): (*PRUNE)、(*跳过)、(*FAIL)和(*F) php: (*BSR_ANYCRLF)
- Recursion (php and perl):
(?R)
,(?0)
and(?1)
,(?-1)
,(?&groupname)
- 递归(php和perl):R(?),(? 0)和(? 1),(? 1),(? groupname)
Common Tasks
常见的任务
- Get a string between two curly braces:
{...}
- 在两个大括号之间获取一个字符串:{…}
- Match (or replace) a pattern except in situations s1, s2, s3...
- 匹配(或替换)一个模式,但在s1、s2、s3中除外。
- How do I find all YouTube video ids in a string using a regex?
- 如何使用regex在字符串中查找所有YouTube视频id ?
- Validation:
- Internet: email addresses, URLs (host/port: regex and non-regex alternatives), passwords
- Internet:电子邮件地址、url(主机/端口:regex和非regex选项)、密码。
- Numeric: a number, min-max ranges (such as 1-31), phone numbers, date
- 数字:数字、最小值范围(如1-31)、电话号码、日期
- Parsing HTML with regex: See "General Information > When not to use Regex"
- 使用regex解析HTML:参见“不使用regex时的一般信息>”
- 验证:Internet:电子邮件地址、url(主机/端口:regex和非regex替代品)、密码:数字、最小-最大值范围(如1-31)、电话号码、日期解析HTML与regex:参见“不使用regex时的一般信息>”
Advanced Regex-Fu
先进Regex-Fu
- Strings and numbers:
- Regular expression to match a line that doesn't contain a word?
- 正则表达式匹配不包含单词的行吗?
- How does this PCRE pattern detect palindromes?
- PCRE模式如何检测回文?
- Match strings whose length is a fourth power
- 匹配长度为四次方的字符串
- How does this regex find triangular numbers?
- 这个正则表达式如何找到三角形数字?
- How to determine if a number is a prime with regex?
- 如何确定一个带regex的数字是否是质数?
- How to match the middle character in a string with regex?
- 如何将字符串中的中间字符与regex匹配?
- 字符串和数字:正则表达式匹配不包含单词的行吗?PCRE模式如何检测回文?匹配长度为四次方的字符串这个regex如何找到三角形的数字?如何确定一个带regex的数字是否是质数?如何将字符串中的中间字符与regex匹配?
- Other:
- How can we match a^n b^n with Java regex?
- 我们如何能匹配一个^ n b ^ n与Java正则表达式?
- Match nested brackets
- Using a recursive pattern php, perl
- 使用php、perl递归模式
- Using balancing groups .net
- 使用平衡组。net
- 使用递归模式php匹配嵌套的括号,使用平衡组。net匹配perl
- “Vertical” regex matching in an ASCII “image”
- ASCII“图像”中的“垂直”regex匹配
- List of highly up-voted regex questions on Code Golf
- 关于代码高尔夫的高级的regex问题列表。
- How to make two quantifiers repeat the same number of times?
- 如何使两个量词重复相同的次数?
- An impossible-to-match regular expression:
(?!a)a
- 一个不可能匹配的正则表达式:(?!)a
- Match/delete/replace
this
except in contexts A, B and C - 除了在上下文中A、B和C中匹配/删除/替换
- Match nested brackets with regex without using recursion or balancing groups?
- 使用regex匹配嵌套的括号,而不使用递归或平衡组?
- 其他:我们如何能匹配一个^ n b ^ n与Java正则表达式?使用递归模式php匹配嵌套的括号,使用平衡组。net“垂直”regex匹配ASCII“图像”列表中的高度向上投票的regex问题如何使两个量词重复相同的次数?一个不可能匹配的正则表达式:(?!a)匹配/删除/替换它,除了在上下文a中,B和C与正则表达式匹配,不使用递归或平衡组?
Flavor-Specific Information
Flavor-Specific信息
(Except for those marked with *
, this section contains non-Stack Overflow links.)
(除标有*的外,本节包含非堆栈溢出链接。)
- Java
- Official documentation: Pattern Javadoc, Oracle's regular expressions tutorial
- 官方文档:模式Javadoc, Oracle的正则表达式教程
- The differences between functions in
java.util.regex.Matcher
:-
matches()
): The match must be anchored to both input-start and -end - match():匹配必须同时锚定输入-开始和-结束
-
find()
): A match may be anywhere in the input string (substrings) - find()):匹配可能位于输入字符串的任何位置(子字符串)
-
lookingAt()
: The match must be anchored to input-start only - lookingAt():比赛必须只锚定在输入-开始
- (For anchors in general, see the section "Anchors")
- (一般锚点见“锚”一节)
-
- java.util.regex中的函数之间的差异。Matcher: matches(): match必须同时锚定到input-start和-end find(): match可能在输入字符串(子字符串)lookingAt()中的任何位置:匹配必须锚定到仅存于input-start(一般锚点,请参阅“anchor”一节)
- The only
java.lang.String
functions that accept regular expressions:matches(s)
,replaceAll(s,s)
,replaceFirst(s,s)
,split(s)
,split(s,i)
- 唯一. lang。接受正则表达式的字符串函数:matches(s)、replaceAll(s,s)、replaceFirst(s,s)、split(s)、split(s,i)
- *An (opinionated and) detailed discussion of the disadvantages of and missing features in
java.util.regex
- 关于java.util.regex的缺点和缺少特性的详细讨论。
- Java官方文档:Pattern Javadoc, Oracle的正则表达式教程,介绍Java .util.regex中的函数之间的差异。Matcher: matches(): match必须同时锚定到input-start和-end find(): match可能位于输入字符串(子字符串)lookingAt()中的任何位置:匹配必须锚定到仅用于input-start(对于一般锚点,请参见“anchor”一节)的惟一java.lang。接受正则表达式的字符串函数:matches(s)、replaceAll(s,s)、replaceFirst(s,s)、split(s)、split(s,i) *An (opinionated and)详细讨论了java.util.regex中特性的缺点和缺失
- .NET
- How to read a .NET regex with look-ahead, look-behind, capturing groups and back-references mixed together?
- 如何阅读。net regex,它包含了前向、后向、捕获组和后向引用混合在一起?
- .NET如何通过查找、查找、捕获组和反向引用来读取。net regex ?
- Official documentation:
- Boost regex engine: General syntax, Perl syntax (used by TextPad, Sublime Text, UltraEdit, ...???)
- Boost regex引擎:通用语法、Perl语法(用于TextPad、高级文本、超文本编辑、…??)
- JavaScript 1.5 general info and RegExp object
- JavaScript 1.5通用信息和RegExp对象
- .NET MySQL Oracle Perl5 version 18.2
- .NET MySQL Oracle Perl5版本18.2
- PHP: pattern syntax,
preg_match
- PHP:模式语法,preg_match
- Python: Regular expression operations,
search
vsmatch
, how-to - Python:正则表达式操作、搜索vs match、how-to
- Splunk: regex terminology and syntax and regex command
- Splunk: regex术语、语法和regex命令
- Tcl: regex syntax, manpage,
regexp
command - Tcl: regex语法,manpage, regexp命令
- Visual Studio Find and Replace
- 找到并替换Visual Studio
- 官方文档:Boost regex引擎:通用语法、Perl语法(用于TextPad、崇高文本、超文本、…??)JavaScript 1.5一般信息和RegExp对象。net MySQL Oracle Perl5版本18.2 PHP:模式语法、preg_match Python:正则表达式操作、搜索vs匹配、如何操作Splunk: regex术语和语法和regex命令Tcl: regex语法、manpage、RegExp命令Visual Studio查找和替换。
General information
一般信息
(Links marked with *
are non-Stack Overflow links.)
(标有*的链接是非堆栈溢出链接。)
- Other general documentation resources: Learning Regular Expressions, *Regular-expressions.info, *Wikipedia entry, *RexEgg, Open-Directory Project
- 其他通用文档资源:学习正则表达式、*正则表达式.info、*Wikipedia条目、*RexEgg、Open-Directory项目
- DFA versus NFA
- DFA与NFA
- Generating Strings matching regex
- 生成字符串匹配正则表达式
- Books: Jeffrey Friedl's Mastering Regular Expressions
- 书籍:杰弗里·弗里德掌握正则表达式
- When to not use regular expressions:
- Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. (blog post written by Stack Overflow's founder)*
- 有些人遇到问题时,会想:“我知道,我会用正则表达式。”现在他们有两个问题。(Stack Overflow的创始人写的博客文章)*
- Do not use regex to parse HTML:
- Don't. Please, just don't
- 不喜欢。请不要
- Well, maybe...if you're really determined (other answers in this question are also good)
- 好吧,也许……如果你真的下定决心了(这个问题的其他答案也不错)
- 不要使用regex解析HTML:不要。求你了,只是不太好,也许……如果你真的下定决心了(这个问题的其他答案也不错)
- 当不使用正则表达式时:有些人遇到问题时,想“我知道,我会用正则表达式。”现在他们有两个问题。(Stack Overflow的创始人写的博客文章)*不要使用regex解析HTML:不要。求你了,只是不太好,也许……如果你真的下定决心了(这个问题的其他答案也不错)
Examples of regex that can cause regex engine to fail
regex的例子可能导致regex引擎失败
- Why does this regular expression kill the Java regex engine?
- 为什么这个正则表达式会杀死Java regex引擎?
Tools: Testers and Explainers
工具:测试人员和讲解员
(This section contains non-Stack Overflow links.)
(此部分包含非堆栈溢出链接。)
-
Online (* includes replacement tester, + includes split tester):
在线(*包含更换测试仪,+包含拆分测试仪):
- Debuggex (Also has a repository of useful regexes) javascript, python, pcre
- Debuggex(也有一个有用的regexes库)javascript、python、pcre
- *Regular Expressions 101 php, pcre, python, javascript
- *正则表达式101 php、pcre、python、javascript
- Regex Pal, regular-expressions.info javascript
- Regex Pal,regular-expressions.info javascript
- Rubular ruby RegExr Regex Hero dotnet
- ruruby RegExr RegExr Regex Hero dotnet。
- *+ regexstorm.net .net
- * + regexstorm.net . net
- *RegexPlanet: Java java, Go go, Haskell haskell, JavaScript javascript, .NET dotnet, Perl perl php PCRE php, Python python, Ruby ruby, XRegExp xregexp
- *RegexPlanet: Java、Go Go、Haskell Haskell、JavaScript、.NET、Perl php PCRE php、Python、Ruby、XRegExp XRegExp
-
freeformatter.com
xregexp - freeformatter.com xregexp
- *+
regex.larsolavtorvik.com
php PCRE and POSIX, javascript - *+regex.larsolavtorvik.com php PCRE和POSIX, javascript。
- Refiddle javascript ruby .net
- Refiddle javascript ruby . net
-
Offline:
离线:
- Microsoft Windows: RegexBuddy (analysis), RegexMagic (creation), Expresso (analysis, creation, free)
- Microsoft Windows: RegexBuddy(分析)、RegexMagic(创建)、Expresso(分析、创建、免费)
#1
672
The Stack Overflow Regular Expressions FAQ
Online tutorials
在线教程
- RegexOne
- RegexOne
- Regular Expressions Info
- 正则表达式的信息
Quantifiers
量词
- Zero-or-more:
*
:greedy,*?
:reluctant,*+
:possessive - 零个或多个:*:贪婪、* ?:* +:占有欲很强,不情愿
- One-or-more:
+
:greedy,+?
:reluctant,++
:possessive - 一个或多个:+:贪婪,+ ?:不情愿,+ +:所有格
?
:optional (zero-or-one)- :可选(0或1)
- Min/max ranges (all inclusive):
{n,m}
:between n & m,{n,}
:n-or-more,{n}
:exactly n - Min/max ranges (all): {n,m}:between n & m, {n,}:n-or-more, {n}:exactly n
- Differences between greedy, reluctant (a.k.a. "lazy", "ungreedy") and possessive quantifier:
- Greedy vs. Reluctant vs. Possessive Quantifiers
- 贪婪,不情愿,占有,量词
- In-depth discussion on the differences between greedy versus non-greedy
- 深入探讨了贪婪与非贪婪的区别
- What's the difference between
{n}
and{n}?
- {n}和{n}之间有什么区别?
- Can someone explain Possessive Quantifiers to me? php, perl, java, ruby
- 有人能给我解释一下所有格的量词吗?php、perl、java、ruby
- Emulating possessive quantifiers .net
- 模拟所有格量词。net
- Non-Stack Overflow references: From Oracle, regular-expressions.info
- 非堆栈溢出引用:来自Oracle,正则表达式。info
- 贪婪、不情愿之间的区别。“懒”、“不贪心”和所有格量词:贪婪与不情愿与占有量词深入探讨贪婪与非贪婪的区别{n}与{n}的区别是什么?有人能给我解释一下所有格的量词吗?php, perl, java, ruby仿真所有格。net非堆栈溢出引用:来自Oracle,正则表达式。info。
Character Classes
字符类
- What is the difference between square brackets and parentheses?
- 方括号和圆括号的区别是什么?
-
[...]
: any one character,[^...]
: negated/any character but - […:任何一个字符(^……]:否定/任何字符
-
[^]
matches any one character including newlines javascript - (^)匹配任何一个字符包括换行javascript
-
[\w-[\d]]
/[a-z-[qz]]
: set subtraction .net, xml-schema, xpath, JGSoft - [\w-[\d]] / [a-z-[qz]:设置减法。net、xml-schema、xpath、JGSoft
-
[\w&&[^\d]]
: set intersection java, ruby 1.9+ - (\ w ^ \[d]]:设置交叉java,ruby 1.9 +
-
[[:alpha:]]
:POSIX character classes - [[:α:]]:POSIX字符类
-
Why do
[^\\D2]
,[^[^0-9]2]
,[^2[^0-9]]
get different results in Java? java - 为什么[^ \ \ D2]、[^ ^ 0 - 9][2],[^ 2[^ 0 - 9]]在Java中得到不同的结果?java
- Shorthand:
- Digit:
\d
:digit,\D
:non-digit - 数字:\ d:\ d:non-digit数字
- Word character (Letter, digit, underscore):
\w
:word character,\W
:non-word character - 单词字符(字母、数字、下划线):只\w:单词字符,只\w:非单词字符
- Whitespace:
\s
:whitespace,\S
:non-whitespace - 空白:\ s:空格、\ s:非空
- Digit:
- 速记:只\d:只\d:只\d:非位字字符(字母、数字、下划线):只\w:字字符,只\w:非字字符空格:只\s:空格,只\s:非空格
- Unicode categories (
\p{L}, \P{L}
, etc.) - Unicode类别(\p{L}, \p{L},等等)
Escape Sequences
转义序列
- Horizontal whitespace:
\h
:space-or-tab,\t
:tab - 水平空白:\ h:space-or-tab \ t:选项卡
- Newlines:
\r
,\n
:carriage return and line feed- \r \n:回车和换行
-
\R
:generic newline php - \ R:通用php换行符
- 换行:\r, \n:回车和换行:通用换行php
- Negated whitespace sequences:
\H
:Non horizontal whitespace character,\V
:Non vertical whitespace character,\N
:Non line feed character pcre php5 java-8 - 负空格序列:\H:非水平空格字符,\V:非垂直空格字符,\N:非换行字符pcre php5 java-8
- Other:
\v
:vertical tab,\e
:the escape character - 其他:\v:竖屏,\e:转义字符
Anchors
锚
-
^
:start of line/input,\b
:word boundary, and\B
:non-word boundary,$
:end of line/input - ^:开始的行/输入,\ b:单词边界,\ b:非单词边界,美元:行结束/输入
-
\A
:start of input,\Z
:end of input php, perl, ruby - \A:开始输入,\Z:结束输入php, perl, ruby
-
\z
:the very end of input (\Z
in Python) .net, php, pcre, java, ruby, icu, swift, objective-c - net, php, pcre, java, ruby, icu, swift, objective-c
-
\G
:start of match php, perl, ruby - \G:开始匹配php、perl、ruby
(Also see "Flavor-Specific Information → Java → The functions in Matcher
")
(也看到“Flavor-Specific信息→Java→匹配器”功能)
Groups
组
-
(...)
:capture group,(?:)
:non-capture group- Why is my repeating capturing group only capturing the last match?
- 为什么我的重复捕获组只捕获最后一个匹配?
- (…):捕获组,(?:):非捕获组为什么我的重复捕获组只捕获最后一个匹配?
-
\1
:backreference and capture-group reference,$1
:capture group reference- What's the meaning of a number after a backslash in a regular expression?
- 正则表达式中的反斜杠后面的数字是什么意思?
-
\g<1>123
:How to follow a numbered capture group, such as\1
, with a number?: python - \g<1>123:如何跟随编号捕获组,例如\1,使用编号?:python
- \1:反向引用和捕获组引用,$1:捕获组引用正则表达式中反斜杠后的数字的含义是什么?\g<1>123:如何跟随编号捕获组,例如\1,使用编号?:python
- What does a subpattern
(?i:regex)
mean? - 子模式(?i:regex)是什么意思?
- What does the 'P' in
(?P<group_name>regexp)
mean? -
P (?P
regexp)中的“P”是什么意思? -
(?>)
:atomic group or independent group,(?|)
:branch reset- Equivalent of branch reset in .NET/C# .net
- 相当于在.net / c# .net中重置分支
- (?>):原子组或独立组(?|):在.net / c# .net中,分支复位等价于分支复位
- Named capture groups:
- General named capturing group reference at
regular-expressions.info
- 一般命名为捕获组参考在常规表达。info
-
java:
(?<groupname>regex)
: Overview and naming rules (Non-Stack Overflow links) -
java: (?
regex):概述和命名规则(非堆栈溢出链接) - Other languages:
(?P<groupname>regex)
python,(?<groupname>regex)
.net,(?<groupname>regex)
perl,(?P<groupname>regex)
and(?<groupname>regex)
php -
其他语言:(?P
regex) python、(? regex) .net、(? regex) perl、(?P regex)和(? regex) php
- General named capturing group reference at
-
命名的捕获组:一般命名的捕获组引用在正则表达式。info java: (?
regex):概述和命名规则(非堆栈溢出链接)其他语言:(?P regex) python, (? 正则表达式). perl, (? 正则表达式),(? regex)和(? regex) php。
Lookarounds
看看
- Lookaheads:
(?=...)
:positive,(?!...)
:negative - 超前:(? =…):积极的,(? !…):消极
- Lookbehinds:
(?<=...)
:positive,(?<!...)
:negative (not supported by javascript) - (?<=…):正数(?
- Lookbehind limits in:
- Lookbehinds need to be constant-length php, perl, python, ruby
- 后面的代码必须是常量长度的php、perl、python、ruby
-
Lookarounds of limited length
{0,n}
java - 有限长度{0,n} java的查找程序
- Variable length lookbehinds are allowed .net
- 可变长度的后视镜是允许的。net
- 在:Lookbehind限值必须是恒长php、perl、python、ruby等长度有限的{0,n} java可变长度的Lookbehind值是允许的。net
- Lookbehind alternatives:
-
Using
\K
php, perl (Flavors that support\K
) - 使用\K php、perl(支持\K)
-
Alternative regex module for Python python
- The hacky way
- 出租汽车司机的方式
- JavaScript negative lookbehind equivalents External link
- JavaScript负的查找隐藏对等的外部链接
- Python的另一个regex模块,JavaScript负查找隐藏等效的外部链接
-
Using
- 查找替代方案:使用\K php、perl(支持\K) Python的可选regex模块,使用hacky方式JavaScript负查找等效外部链接
Modifiers
修饰符
- Most flavors:
g
:global,i
:case-insensitive,u
:unicode,x
:whitespace-extended - 大多数口味:g:global, i:大小写不敏感,u:unicode, x:whitespac -extended
-
c
:current position perle
:expression php perlo
:once ruby - c:当前位置perl e:expression php perl o:once ruby
-
m
:multiline php perl python javascript .net java,m
:(non)multiline ruby - m:多行php perl python javascript。net java, m:(非)多行ruby
-
s
:single line (not supported by javascript or ruby),s
workaround javascript - s:单行(不支持javascript或ruby), javascript的解决方案
-
S
:study phpU
:ungreedy php r - 学习php U:不贪心的php r
- How to convert preg_replace e to preg_replace_callback?
- 如何将preg_replace e转换为preg_replace_callback?
- What are inline modifiers?
- 内联修饰词是什么?
- What is '?-mix' in a Ruby Regular Expression
- “是什么?-在Ruby正则表达式中混合
Other:
其他:
-
|
:alternation (OR) operator,.
:any character,[.]
:literal dot character - 变换(或)运算符:任意字符,[。]:文字点性格
- What special characters must be escaped?
- 需要转义哪些特殊字符?
- Control verbs (php and perl):
(*PRUNE)
,(*SKIP)
,(*FAIL)
and(*F)
-
php only:
(*BSR_ANYCRLF)
- php只:(* BSR_ANYCRLF)
-
php only:
- 控制动词(php和perl): (*PRUNE)、(*跳过)、(*FAIL)和(*F) php: (*BSR_ANYCRLF)
- Recursion (php and perl):
(?R)
,(?0)
and(?1)
,(?-1)
,(?&groupname)
- 递归(php和perl):R(?),(? 0)和(? 1),(? 1),(? groupname)
Common Tasks
常见的任务
- Get a string between two curly braces:
{...}
- 在两个大括号之间获取一个字符串:{…}
- Match (or replace) a pattern except in situations s1, s2, s3...
- 匹配(或替换)一个模式,但在s1、s2、s3中除外。
- How do I find all YouTube video ids in a string using a regex?
- 如何使用regex在字符串中查找所有YouTube视频id ?
- Validation:
- Internet: email addresses, URLs (host/port: regex and non-regex alternatives), passwords
- Internet:电子邮件地址、url(主机/端口:regex和非regex选项)、密码。
- Numeric: a number, min-max ranges (such as 1-31), phone numbers, date
- 数字:数字、最小值范围(如1-31)、电话号码、日期
- Parsing HTML with regex: See "General Information > When not to use Regex"
- 使用regex解析HTML:参见“不使用regex时的一般信息>”
- 验证:Internet:电子邮件地址、url(主机/端口:regex和非regex替代品)、密码:数字、最小-最大值范围(如1-31)、电话号码、日期解析HTML与regex:参见“不使用regex时的一般信息>”
Advanced Regex-Fu
先进Regex-Fu
- Strings and numbers:
- Regular expression to match a line that doesn't contain a word?
- 正则表达式匹配不包含单词的行吗?
- How does this PCRE pattern detect palindromes?
- PCRE模式如何检测回文?
- Match strings whose length is a fourth power
- 匹配长度为四次方的字符串
- How does this regex find triangular numbers?
- 这个正则表达式如何找到三角形数字?
- How to determine if a number is a prime with regex?
- 如何确定一个带regex的数字是否是质数?
- How to match the middle character in a string with regex?
- 如何将字符串中的中间字符与regex匹配?
- 字符串和数字:正则表达式匹配不包含单词的行吗?PCRE模式如何检测回文?匹配长度为四次方的字符串这个regex如何找到三角形的数字?如何确定一个带regex的数字是否是质数?如何将字符串中的中间字符与regex匹配?
- Other:
- How can we match a^n b^n with Java regex?
- 我们如何能匹配一个^ n b ^ n与Java正则表达式?
- Match nested brackets
- Using a recursive pattern php, perl
- 使用php、perl递归模式
- Using balancing groups .net
- 使用平衡组。net
- 使用递归模式php匹配嵌套的括号,使用平衡组。net匹配perl
- “Vertical” regex matching in an ASCII “image”
- ASCII“图像”中的“垂直”regex匹配
- List of highly up-voted regex questions on Code Golf
- 关于代码高尔夫的高级的regex问题列表。
- How to make two quantifiers repeat the same number of times?
- 如何使两个量词重复相同的次数?
- An impossible-to-match regular expression:
(?!a)a
- 一个不可能匹配的正则表达式:(?!)a
- Match/delete/replace
this
except in contexts A, B and C - 除了在上下文中A、B和C中匹配/删除/替换
- Match nested brackets with regex without using recursion or balancing groups?
- 使用regex匹配嵌套的括号,而不使用递归或平衡组?
- 其他:我们如何能匹配一个^ n b ^ n与Java正则表达式?使用递归模式php匹配嵌套的括号,使用平衡组。net“垂直”regex匹配ASCII“图像”列表中的高度向上投票的regex问题如何使两个量词重复相同的次数?一个不可能匹配的正则表达式:(?!a)匹配/删除/替换它,除了在上下文a中,B和C与正则表达式匹配,不使用递归或平衡组?
Flavor-Specific Information
Flavor-Specific信息
(Except for those marked with *
, this section contains non-Stack Overflow links.)
(除标有*的外,本节包含非堆栈溢出链接。)
- Java
- Official documentation: Pattern Javadoc, Oracle's regular expressions tutorial
- 官方文档:模式Javadoc, Oracle的正则表达式教程
- The differences between functions in
java.util.regex.Matcher
:-
matches()
): The match must be anchored to both input-start and -end - match():匹配必须同时锚定输入-开始和-结束
-
find()
): A match may be anywhere in the input string (substrings) - find()):匹配可能位于输入字符串的任何位置(子字符串)
-
lookingAt()
: The match must be anchored to input-start only - lookingAt():比赛必须只锚定在输入-开始
- (For anchors in general, see the section "Anchors")
- (一般锚点见“锚”一节)
-
- java.util.regex中的函数之间的差异。Matcher: matches(): match必须同时锚定到input-start和-end find(): match可能在输入字符串(子字符串)lookingAt()中的任何位置:匹配必须锚定到仅存于input-start(一般锚点,请参阅“anchor”一节)
- The only
java.lang.String
functions that accept regular expressions:matches(s)
,replaceAll(s,s)
,replaceFirst(s,s)
,split(s)
,split(s,i)
- 唯一. lang。接受正则表达式的字符串函数:matches(s)、replaceAll(s,s)、replaceFirst(s,s)、split(s)、split(s,i)
- *An (opinionated and) detailed discussion of the disadvantages of and missing features in
java.util.regex
- 关于java.util.regex的缺点和缺少特性的详细讨论。
- Java官方文档:Pattern Javadoc, Oracle的正则表达式教程,介绍Java .util.regex中的函数之间的差异。Matcher: matches(): match必须同时锚定到input-start和-end find(): match可能位于输入字符串(子字符串)lookingAt()中的任何位置:匹配必须锚定到仅用于input-start(对于一般锚点,请参见“anchor”一节)的惟一java.lang。接受正则表达式的字符串函数:matches(s)、replaceAll(s,s)、replaceFirst(s,s)、split(s)、split(s,i) *An (opinionated and)详细讨论了java.util.regex中特性的缺点和缺失
- .NET
- How to read a .NET regex with look-ahead, look-behind, capturing groups and back-references mixed together?
- 如何阅读。net regex,它包含了前向、后向、捕获组和后向引用混合在一起?
- .NET如何通过查找、查找、捕获组和反向引用来读取。net regex ?
- Official documentation:
- Boost regex engine: General syntax, Perl syntax (used by TextPad, Sublime Text, UltraEdit, ...???)
- Boost regex引擎:通用语法、Perl语法(用于TextPad、高级文本、超文本编辑、…??)
- JavaScript 1.5 general info and RegExp object
- JavaScript 1.5通用信息和RegExp对象
- .NET MySQL Oracle Perl5 version 18.2
- .NET MySQL Oracle Perl5版本18.2
- PHP: pattern syntax,
preg_match
- PHP:模式语法,preg_match
- Python: Regular expression operations,
search
vsmatch
, how-to - Python:正则表达式操作、搜索vs match、how-to
- Splunk: regex terminology and syntax and regex command
- Splunk: regex术语、语法和regex命令
- Tcl: regex syntax, manpage,
regexp
command - Tcl: regex语法,manpage, regexp命令
- Visual Studio Find and Replace
- 找到并替换Visual Studio
- 官方文档:Boost regex引擎:通用语法、Perl语法(用于TextPad、崇高文本、超文本、…??)JavaScript 1.5一般信息和RegExp对象。net MySQL Oracle Perl5版本18.2 PHP:模式语法、preg_match Python:正则表达式操作、搜索vs匹配、如何操作Splunk: regex术语和语法和regex命令Tcl: regex语法、manpage、RegExp命令Visual Studio查找和替换。
General information
一般信息
(Links marked with *
are non-Stack Overflow links.)
(标有*的链接是非堆栈溢出链接。)
- Other general documentation resources: Learning Regular Expressions, *Regular-expressions.info, *Wikipedia entry, *RexEgg, Open-Directory Project
- 其他通用文档资源:学习正则表达式、*正则表达式.info、*Wikipedia条目、*RexEgg、Open-Directory项目
- DFA versus NFA
- DFA与NFA
- Generating Strings matching regex
- 生成字符串匹配正则表达式
- Books: Jeffrey Friedl's Mastering Regular Expressions
- 书籍:杰弗里·弗里德掌握正则表达式
- When to not use regular expressions:
- Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. (blog post written by Stack Overflow's founder)*
- 有些人遇到问题时,会想:“我知道,我会用正则表达式。”现在他们有两个问题。(Stack Overflow的创始人写的博客文章)*
- Do not use regex to parse HTML:
- Don't. Please, just don't
- 不喜欢。请不要
- Well, maybe...if you're really determined (other answers in this question are also good)
- 好吧,也许……如果你真的下定决心了(这个问题的其他答案也不错)
- 不要使用regex解析HTML:不要。求你了,只是不太好,也许……如果你真的下定决心了(这个问题的其他答案也不错)
- 当不使用正则表达式时:有些人遇到问题时,想“我知道,我会用正则表达式。”现在他们有两个问题。(Stack Overflow的创始人写的博客文章)*不要使用regex解析HTML:不要。求你了,只是不太好,也许……如果你真的下定决心了(这个问题的其他答案也不错)
Examples of regex that can cause regex engine to fail
regex的例子可能导致regex引擎失败
- Why does this regular expression kill the Java regex engine?
- 为什么这个正则表达式会杀死Java regex引擎?
Tools: Testers and Explainers
工具:测试人员和讲解员
(This section contains non-Stack Overflow links.)
(此部分包含非堆栈溢出链接。)
-
Online (* includes replacement tester, + includes split tester):
在线(*包含更换测试仪,+包含拆分测试仪):
- Debuggex (Also has a repository of useful regexes) javascript, python, pcre
- Debuggex(也有一个有用的regexes库)javascript、python、pcre
- *Regular Expressions 101 php, pcre, python, javascript
- *正则表达式101 php、pcre、python、javascript
- Regex Pal, regular-expressions.info javascript
- Regex Pal,regular-expressions.info javascript
- Rubular ruby RegExr Regex Hero dotnet
- ruruby RegExr RegExr Regex Hero dotnet。
- *+ regexstorm.net .net
- * + regexstorm.net . net
- *RegexPlanet: Java java, Go go, Haskell haskell, JavaScript javascript, .NET dotnet, Perl perl php PCRE php, Python python, Ruby ruby, XRegExp xregexp
- *RegexPlanet: Java、Go Go、Haskell Haskell、JavaScript、.NET、Perl php PCRE php、Python、Ruby、XRegExp XRegExp
-
freeformatter.com
xregexp - freeformatter.com xregexp
- *+
regex.larsolavtorvik.com
php PCRE and POSIX, javascript - *+regex.larsolavtorvik.com php PCRE和POSIX, javascript。
- Refiddle javascript ruby .net
- Refiddle javascript ruby . net
-
Offline:
离线:
- Microsoft Windows: RegexBuddy (analysis), RegexMagic (creation), Expresso (analysis, creation, free)
- Microsoft Windows: RegexBuddy(分析)、RegexMagic(创建)、Expresso(分析、创建、免费)