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.
正则表达式正在遭受给我ZE代码类型的问题和没有解释的糟糕答案。此参考旨在提供指向质量问答的链接。
What's the scope?
This reference is meant for the following languages: php, perl, javascript, python, ruby, java, .net.
此参考适用于以下语言:PHP、Perl、javascrip、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
更多回答
The Stack Overflow Regular Expressions FAQ
See also a lot of general hints and useful links at the regex tag details page.
另请参见regex标记详细信息页面上的许多常规提示和有用的链接。
Online tutorials
在线教程
Quantifiers
量词
- Zero-or-more:
*
: greedy, *?
: reluctant, *+
: possessive
- One-or-more:
+
: greedy, +?
: reluctant, ++
: possessive
- Zero-or-one:
?
: greedy, ??
: reluctant, ?+
: possessive
- Min/max ranges (all inclusive):
{n,m}
: between n & m, {n,}
: n-or-more, {n}
: exactly n
- Differences between greedy, reluctant (a.k.a. "lazy", "ungreedy") and possessive quantifier:
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
[\w-[\d]]
/ [a-z-[qz]]
: set subtraction .net, xml-schema, xpath, JGSoft
[\w&&[^\d]]
: set intersection java, ruby 1.9+, javascript (with v
flag)
[[:alpha:]]
:POSIX character classes
[[:<:]]
and [[:>:]]
Word boundaries
- Why do
[^\\D2]
, [^[^0-9]2]
, [^2[^0-9]]
get different results in Java? java
- Shorthand:
- Unicode categories (
\p{L}, \P{L}
, etc.)
Escape Sequences
转义序列
Anchors
锚
anchor |
matches |
flavors |
^ |
Start of string |
Common* |
^ |
Start of line |
Commonm |
$ |
End of line |
Commonm |
$ |
End of text |
Common* except javascript |
$ |
Very end of string |
javascript*, phpD |
\A |
Start of string |
Common except javascript |
\Z |
End of text |
Common except javascript python |
\Z |
Very end of string |
python |
\z |
Very end of string |
Common except javascript python |
\b |
Word boundary |
Common |
\B |
Not a word boundary |
Common |
\G |
End of previous match |
Common except javascript, python |
Term |
Definition |
Start of string |
At the very start of the string. |
Start of line |
At the very start of the string, and after a non-terminal line terminator. |
Very end of string |
At the very end of the string. |
End of text |
At the very end of the string, and at a terminal line terminator. |
End of line |
At the very end of the string, and at a line terminator. |
Word boundary |
At a word character not preceded by a word character, and at a non-word character not preceded by a non-word character. |
End of previous match |
At a previously set position, usually where a previous match ended. At the very start of the string if no position was set. |
"Common" refers to the following: icu java javascript .net objective-c pcre perl php python swift ruby
“通用”指的是以下内容:icu Java javascrip.net Objective-c pcre Perl php python快速红宝石
* Default |
m
Multi-line mode. |
D
Dollar end only mode.
* 默认|m多线模式。| D Dollar end only mode.
Groups
群组
Lookarounds
环顾四周
Modifiers
修饰词
Other:
其他:
Common Tasks
常见任务
Advanced Regex-Fu
高级Regex-FU
- Strings and numbers:
- Other:
Flavor-Specific Information
特定口味的信息
(Except for those marked with *
, this section contains non-Stack Overflow links.)
(除标有*的链接外,此部分包含非堆栈溢出链接。)
- Java
- .NET
- Official documentation:
- Boost regex engine: General syntax, Perl syntax (used by TextPad, Sublime Text, UltraEdit, ...???)
- JavaScript general info and RegExp object
- .NET MySQL Oracle Perl5 version 18.2
- PHP: pattern syntax,
preg_match
- Python: Regular expression operations,
search
vs match
, how-to
- Rust: crate
regex
, struct regex::Regex
- Splunk: regex terminology and syntax and regex command
- Tcl: regex syntax, manpage,
regexp
command
- Visual Studio Find and Replace
General information
一般信息
(Links marked with *
are non-Stack Overflow links.)
(标有*的链接是非堆栈溢出链接。)
Examples of regex that can cause regex engine to fail
可能导致正则表达式引擎失败的正则表达式示例
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
- *Regular Expressions 101 php, pcre, python, javascript, java, go, c#, rust
- Regex Pal, regular-expressions.info javascript
- Rubular ruby RegExr Regex Hero dotnet
- *+ 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
freeformatter.com
xregexp
- *+
regex.larsolavtorvik.com
php PCRE and POSIX, javascript
Offline:
脱机:
MySQL 8.0: Various syntax changes were made. Note especially the doubling of backslashes in some contexts. (This Answer need further editing to reflect the differences.)
MySQL8.0:进行了各种语法更改。特别要注意的是,在某些情况下,反斜杠是双斜杠。(此答案需要进一步编辑,以反映差异。)
更多回答
Perl has more ((?(
for conditional, for example) but you can read the perl official documentation instead.
Perl有更多的((?(例如,对于条件),但你可以阅读Perl官方文档。
Looks like Refiddle
under the Tools section now points to some online casino site. Probably should be removed.
看起来,工具部分下的Refiddle现在指向了一些在线赌场网站。可能应该把它取下来。
I think the anchors section is incorrect for ruby, as per this
我认为Ruby的锚部分是不正确的,就像这样
我是一名优秀的程序员,十分优秀!