这是一个常见的Q& A的集合。这也是一个社区Wiki,所以大家被邀请参与维护它。
为什么是这样?
regex是给我我的代码类型的问题和穷人的答案,没有解释。此引用意在提供到质量问答的链接。
范围是什么?
此引用适用于以下语言:php,perl,javascript,python,ruby,java,.net。
这可能过于宽泛,但这些语言具有相同的语法。对于特定的功能,有它背后的语言的标签,例如:
>什么是正则表达式平衡组? .net
量词
>零或更多:*
:greedy,[*?:不愿] [需要链接],*+
:possessive
>一个或多个:+
:greedy,[?:reluctant] [需要链接],++
:possessive
> ?
:optional (zero-or-one)
>最小/最大范围(全部):{n,m}
:between n & m,{n,}
:n-or-more,{n}
:exactly n
>贪婪,勉强(又名“懒惰”,“不y”)和占有量词的区别:
> 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}?
> Can someone explain Possessive Quantifiers to me? php,perl,java,ruby
> Emulating possessive quantifiers .net
>非stackoverflow参考:从Oracle,regular-expressions.info
字符类
> What is the difference between square brackets and parentheses?
> [...]
:任何一个字符,[^...]
:否定/任何字符
> [^]
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字符类
> Why do [^\\D2]
,[^[^0-9]2]
,[^2[^0-9]]
get different results in Java? java
>短手:
> Digit:\d
:digit,\D
:non-digit
>字符(字母,数字,下划线):\w
:word character,\W
:non-word character
>空白:\s
:whitespace,\S
:non-whitespace
> Unicode categories (\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,python,ruby
> \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
> \g<1>123
:How to follow a numbered capture group,such as \1
,with a number?: python
> What does a subpattern (?i:regex)
mean?
> What does the ‘P’ in (?P<group_name>regexp)
mean?
> (?>)
:atomic group或independent group,(?|)
:branch reset
> Equivalent of branch reset in .NET/C# .net
>命名捕获组:
> java:(?< groupname> regex):Overview和naming rules(Non stackoverflow links)
>其他语言:(?P<groupname>regex)
python,(?<groupname>regex)
.net,(?<groupname>regex)
perl,(< group name> regex)和(?< groupname> regex)php
环境
> Lookaheads:(?=...)
:positive,(?!...)
:negative
> Lookbehinds:(?<=...)
:positive,(?<!...)
:negative(不受javascript支持)
> Lookbehind限制:
> Lookbehinds need to be constant-length php,perl,python,ruby
> Lookarounds of limited length {0,n}
java
> Variable length lookbehinds are allowed .net
> Lookbehind替代方案:
> Using \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
:expression php perl o
:once ruby
> m
:multiline php perl python javascript .net java,m
:(non)multiline ruby
> s
:single line(不支持javascript或ruby),s
workaround javascript
> S
:study php r
> How to convert preg_replace e to preg_replace_callback?
> What are inline modifiers?
> What is ‘?-mix’ in a Ruby Regular Expression
其他:
> |
:alternation (OR) operator,.
:any character,[.]
:literal dot character
> What special characters must be escaped?
>控制动词(php和perl):(*PRUNE)
,(*SKIP)
,(*FAIL)
and (*F)
> php only:(*BSR_ANYCRLF)
>递归(php和perl):(?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 to find all Youtube video ids in a string using a regex?
>验证:
> Internet:email addresses,urls(主机/端口:regex和non-regex替代方案),passwords
> Numeric:a number,min-max ranges (such as 1-31),phone numbers,date
>使用regex解析HTML:请参见“常规信息>何时不使用Regex”
高级Regex-Fu
>字符串和数字:
> @L_502_180@
> How does this PCRE pattern detect palindromes?
> 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?
> How to match the middle character in a string with regex?
>其他:
> How can we match a^n b^n with Java regex?
>匹配嵌套括号
> Using a recursive pattern php,perl
> Using balancing groups .net
> “Vertical” regex matching in an ASCII “image”
> List of highly up-voted regex questions on Code Golf
> How to make two quantifiers repeat the same number of times?
> An impossible-to-match regular expression: (?!a)a
> Match/delete/replace this
except in contexts A,B and C
风味特异性信息
> Java
>官方文档:Pattern Javadoc,Oracle’s regular expressions tutorial
> java.util.regex.Matcher
中的功能之间的区别:
> matches()
):匹配必须锚定到input-start和-end
> find()
):匹配可以在输入字符串(子串)中的任何位置
> lookingAt()
:匹配必须仅锚定到输入启动
>(对于锚,一般来说,参见“锚点”一节)
>接受正则表达式的唯一的java.lang.String
函数: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
> .NET
>官方文档:
> Boost regex引擎:General syntax,Perl syntax(由TextPad,Sublime Text,UltraEdit,…使用)
> JavaScript 1.5 general info和RegExp object
> .NET MySQL Oracle Perl5 version 18.2
> PHP:pattern syntax,preg_match
> Python:Regular expression operations,search
vs match
,how-to
> Splunk:regex terminology and syntax和regex command
> Tcl:regex syntax,manpage,regexp
command
一般信息
>其他一般文档资源:Learning Regular Expressions,* Regular-expressions.info,* Wikipedia entry,* RexEgg,Open-Directory Project
> DFA versus NFA
> Generating Strings matching regex
>图书:Jeffrey Friedl’s * Mastering Regular Expressions
>何时不使用正则表达式:
> * Some people,when confronted with a problem,think “I know,I’ll use regular expressions.” Now they have two problems.(博客由Stack Overflow的创始人写)
>不要使用正则表达式来解析HTML:
> Don’t.Please,just don’t
> Well,maybe…if you’re really determined(这个问题的其他答案也不错)
可能导致正则表达式引擎失败的正则表达式示例
> Why does this regular expression kill the Java regex engine?
工具:测试者和解释者
(此部分包含非Stack Overflow链接。)
> Debuggex(还有一个有用的正则表达式库)javascript,python,pcre
> * Regular Expressions 101 php,pcre,python,javascript
> Regex Pal,* regular-expressions.info javascript
> Rubular ruby RegExr Regex Hero dotnet
> * regexstorm.net .net
> * RegexPlanet:Java java,Go go,Haskell haskell,php javascript,.NET dotnet,Perl perl php PCRE php,Python python,Ruby ruby,XRegExp xregexp
> freeformatter.com
xregexp
> * regex.larsolavtorvik.com
php PCRE和POSIX,javascript
> Refiddle javascript ruby .net
>离线:
> Microsoft Windows:RegexBuddy(分析),RegexMagic(创建),Expresso(分析,创建,免费)