这是常见问答的集合。这也是社区Wiki,因此邀请每个人参与维护。
regex正在遭受给我代码类型的问题和糟糕的答案,没有任何解释。此参考旨在提供质量问答的链接。
本参考文献适用于以下语言:php,perl,javascript,python,ruby,java,.net。
这可能过于宽泛,但这些语言共享相同的语法。对于特定功能,它背后的语言标签,例如:
在线教程
量词
*
:greedy,*?
:reluctant,*+
:possessive+
:greedy,+?
:reluctant,++
:possessive?
:optional (zero-or-one){n,m}
:between n & m,{n,}
:n-or-more,{n}
:exactly n{n}
and {n}?
Can someone explain Possessive Quantifiers to me? php,perl,java,ruby
Emulating possessive quantifiers .net
非Stack Overflow参考:来自Oracle,regular-expressions.info角色类
[...]
:任何一个角色,[^...]
:否定/任何角色但是[^]
matches any one character including newlines javascript[\w-[\d]]
/ [a-z-[qz]]
:设置减法.net,xml-schema,xpath,JGSoft[\w&&[^\d]]
: set intersection java,ruby 1.9+[[:alpha:]]
:POSIX字符类[^\\D2]
, [^[^0-9]2]
, [^2[^0-9]]
get different results in Java? java\d
:digit,\D
:non-digit
单词字符(字母,数字,下划线):\w
:word character,\W
:non-word character
空白:\s
:whitespace,\S
:non-whitespace\p{L}, \P{L}
, etc.)转义序列
\h
:space-or-tab,\t
:tab\r
, \n
:carriage return and line feed
\R
:generic newline php\H
:Non horizontal whitespace character, \V
:Non vertical whitespace character, \N
:Non line feed character pcre php5 java-8\v
:vertical tab,\e
:the escape character锚
^
:start of line/input,\b
:word boundary, and \B
:non-word boundary,$
:end of line/input\A
:start of input, \Z
:end of input php,perl,ruby\z
:the very end of input(Python中的\Z
).net,php,pcre,java,ruby,icu,swift,objective-c\G
:start of match php,perl,ruby(另请参阅“风味特定信息→Java→Matcher
中的功能”)
组
(...)
: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(?i:regex)
mean?(?P<group_name>regexp)
mean?(?>)
:atomic group或independent group,(?|)
:branch reset
Equivalent of branch reset in .NET/C# .netregular-expressions.info
java:(?<groupname>regex)
:Overview和naming rules(非Stack Overflow链接)
其他语言:(?P<groupname>regex)
python,(?<groupname>regex)
.net,(?<groupname>regex)
perl,(?P<groupname>regex)
和(?<groupname>regex)
phpLokaraunds
(?=...)
:positive,(?!...)
:negative(?<=...)
:positive,(?<!...)
:negative(不支持javascript){0,n}
java
Variable length lookbehinds are allowed .net\K
php,perl(Flavors that support \K
)
Alternative regex module for Python python
The hacky way
JavaScript negative lookbehind equivalents External link修饰符
g
:global i
:case-insensitive u
:unicode x
:whitespace-extended c
:current position perl e
:expressiono
:once ruby m
:multiline php perl,python javascriptm
:(non)multiline),ruby s
:single lines
workaround javascript S
:studyU
:ungreedy其他:
|
:alternation (OR) operator和.
:any character):[.]
:literal dot character,What special characters must be escaped?,php
仅限perl:(*PRUNE)
(*SKIP)
,(*FAIL)
and (*F)
,php常见任务
(*BSR_ANYCRLF)
(?R)
,(?0)
and (?1)
(主机/港口:(?-1)
和(?&groupname)
替代品),Get a string between two curly braces: {...}
数字:Match (or replace) a pattern except in situations s1, s2, s3...,How do I find all YouTube video ids in a string using a regex?,email addresses,URLs
使用正则表达式解析HTML:请参阅“常规信息>何时不使用正则表达式”高级正则表达式
风味特定信息
(除了那些标有php的内容外,此部分包含非Stack Overflow链接。)
(?!a)a
函数:Match/delete/replace this
except in contexts A, B and C,Match nested brackets with regex without using recursion or balancing groups?,*
,Pattern Javadoc,Oracle's regular expressions tutorial
* java.util.regex.Matcher
matches()
find()
,lookingAt()
(由TextPad使用,Sublime Text,UltraEdit,...... ???)
JavaScript 1.5 java.lang.String
和matches(s)
replaceAll(s,s)
replaceFirst(s,s)
split(s)
split(s,i)
PHP:An (opinionated and) detailed discussion of the disadvantages of and missing features in java.util.regex
,How to read a .NET regex with look-ahead, look-behind, capturing groups and back-references mixed together?
Python:General syntax,Perl syntax,general info
Splunk:RegExp object和.NET
Tcl:MySQL,Oracle,Perl5 version 18.2
pattern syntax一般信息
(标记为preg_match
的链接是非Stack Overflow链接。)
search
vs match
,* how-to,* regex terminology and syntax,regex commandregexp
command*
创始人撰写的博文)*
不要使用正则表达式来解析HTML:
Learning Regular Expressions。 Regular-expressions.info
Wikipedia entry(这个问题的其他答案也很好)可能导致正则表达式引擎失败的正则表达式示例
工具:测试人员和解说员
(此部分包含非Stack Overflow链接。)
freeformatter.com
(创作),xregexp(分析,创作,免费)