正则表达式的高级语法

Advanced Syntax Reference
正则高级语法参考

Grouping and Backreferences
分组查找与后向引用
Syntax
语法
Description
描述
Example
示例
(regex)Round brackets group the regex between them. They capture the text matched by the regex inside them that can be reused in a backreference, and they allow you to apply regex operators to the entire grouped regex.(abc){3} matches abcabcabc. First group matches abc.
(?:regex)Non-capturing parentheses group the regex so you can apply regex operators, but do not capture anything and do not create backreferences.(?:abc){3} matches abcabcabc. No groups.
/1 through /9Substituted with the text matched between the 1st through 9th pair of capturing parentheses. Some regex flavors allow more than 9 backreferences.(abc|def)=/1 matches abc=abc or def=def, but not abc=def or def=abc.
Modifiers
正则修饰符
Syntax
语法
Description
描述

Example

示例

(?i)Turn on case insensitivity for the remainder of the regular expression. (Older regex flavors may turn it on for the entire regex.)
指明其后面的正则表达式将会忽略大小写进行匹配(老的正则特点也许是指明整个表达式是忽略大小写)
te(?i)st matches teST but not TEST.
(?-i)Turn off case insensitivity for the remainder of the regular expression.
指明其后面的正则表达式将不会忽略大小写也就是大小写敏感的。(与上面的刚好相反)
(?i)te(?-i)st matches TEst but not TEST.
(?s)Turn on "dot matches newline" for the remainder of the regular expression. (Older regex flavors may turn it on for the entire regex.)
指明后面的正则表达式里的“.”将匹配换行符,(老的正则特点也许是对整个表达式生效)
 (ABC)(?s).*可以匹配“DEABCMN/r/nBCD”里的“ABCMN/r/nBCD”
(?-s)

Turn off "dot matches newline" for the remainder of the regular expression.

指明后续的表达式里的“.”不匹配换行符

 (ABC)(?s).*可以匹配“DEABCMN/r/nBCD”里的“ABCMN”
(?m)Caret and dollar match after and before newlines for the remainder of the regular expression. (Older regex flavors may apply this to the entire regex.)
 
(?-m)Caret and dollar only match at the start and end of the string for the remainder of the regular expression. 
(?x)Turn on free-spacing mode to ignore whitespace between regex tokens, and allow # comments. 
(?-x)Turn off free-spacing mode. 
(?i-sm)Turns on the options "i" and "m", and turns off "s" for the remainder of the regular expression. (Older regex flavors may apply this to the entire regex.) 
(?i-sm:regex)Matches the regex inside the span with the options "i" and "m" turned on, and "s" turned off.(?i:te)st matches TEst but not TEST.
Atomic Grouping and Possessive Quantifiers
SyntaxDescriptionExample
(?>regex)Atomic groups prevent the regex engine from backtracking back into the group (forcing the group to discard part of its match) after a match has been found for the group. Backtracking can occur inside the group before it has matched completely, and the engine can backtrack past the entire group, discarding its match entirely. Eliminating needless backtracking provides a speed increase. Atomic grouping is often indispensable when nesting quantifiers to prevent a catastrophic amount of backtracking as the engine needlessly tries pointless permutations of the nested quantifiers.x(?>/w+)x is more efficient than x/w+x if the second x cannot be matched.
?+, *+, ++ and {m,n}+Possessive quantifiers are a limited yet syntactically cleaner alternative to atomic grouping. Only available in a few regex flavors. They behave as normal greedy quantifiers, except that they will not give up part of their match for backtracking.x++ is identical to (?>x+)
Lookaround
SyntaxDescriptionExample
(?=regex)Zero-width positive lookahead. Matches at a position where the pattern inside the lookahead can be matched. Matches only the position. It does not consume any characters or expand the match. In a pattern like one(?=two)three, both two and three have to match at the position where the match of one ends.t(?=s) matches the second t in streets.
(?!regex)Zero-width negative lookahead. Identical to positive lookahead, except that the overall match will only succeed if the regex inside the lookahead fails to match.t(?!s) matches the first t in streets.
(?<=text)Zero-width positive lookbehind. Matches at a position to the left of which text appears. Since regular expressions cannot be applied backwards, the test inside the lookbehind can only be plain text. Some regex flavors allow alternation of plain text options in the lookbehind.(?<=s)t matches the first t in streets.
(?<!text)Zero-width negative lookbehind. Matches at a position if the text does not appear to the left of that position.(?<!s)t matches the second t in streets.
Continuing from The Previous Match
SyntaxDescriptionExample
/GMatches at the position where the previous match ended, or the position where the current match attempt started (depending on the tool or regex flavor). Matches at the start of the string during the first match attempt./G[a-z] first matches a, then matches b and then fails to match in ab_cd.
Conditionals
SyntaxDescriptionExample
(?(?=regex)then|else)If the lookahead succeeds, the "then" part must match for the overall regex to match. If the lookahead fails, the "else" part must match for the overall regex to match. Not just positive lookahead, but all four lookarounds can be used. Note that the lookahead is zero-width, so the "then" and "else" parts need to match and consume the part of the text matched by the lookahead as well.(?(?<=a)b|c) matches the second b and the first c in babxcac
(?(1)then|else)If the first capturing group took part in the match attempt thus far, the "then" part must match for the overall regex to match. If the first capturing group did not take part in the match, the "else" part must match for the overall regex to match.(a)?(?(1)b|c) matches ab, the first c and the second c in babxcac
Comments
SyntaxDescriptionExample
(?#comment)Everything between (?# and ) is ignored by the regex engine.a(?#foobar)b matches ab
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
正则表达式是一种用于匹配和操作字符串的强大工具。它使用特定的语法规则来描述字符串的模式。以下是正则表达式的一些基本语法规则: 1. 字符匹配:正则表达式可以直接匹配指定的字符。例如,正则表达式`a`可以匹配字符串中的字符`a`。 2. 字符类:使用方括号`[]`来定义一个字符类,表示匹配其中的任意一个字符。例如,正则表达式`[abc]`可以匹配字符串中的字符`a`、`b`或`c`。 3. 范围类:在字符类中可以使用连字符`-`来表示一个范围。例如,正则表达式`[a-z]`可以匹配任意小写字母。 4. 反向字符类:在字符类的开头使用`^`表示反向字符类,表示匹配除了字符类中的字符之外的任意字符。例如,正则表达式`[^0-9]`可以匹配任意非数字字符。 5. 元字符:正则表达式中有一些特殊的字符,称为元字符,具有特殊的含义。例如,`.`表示匹配任意字符,`\d`表示匹配任意数字,`\s`表示匹配任意空白字符。 6. 重复次数:使用`*`表示匹配前面的元素零次或多次,使用`+`表示匹配前面的元素一次或多次,使用`?`表示匹配前面的元素零次或一次。例如,正则表达式`a*`可以匹配零个或多个字符`a`。 7. 分组:使用圆括号`()`来创建一个分组,可以对分组内的元素进行重复次数的控制。例如,正则表达式`(ab)+`可以匹配一个或多个连续的字符串`ab`。 8. 反向引用:使用`\数字`来引用前面的分组,表示匹配与该分组相同的内容。例如,正则表达式`(ab)\1`可以匹配两个连续的字符串`abab`。 9. 边界匹配:使用`^`表示匹配字符串的开头,使用`$`表示匹配字符串的结尾。例如,正则表达式`^\d+$`可以匹配一个或多个数字组成的字符串。 10. 贪婪匹配和非贪婪匹配:默认情况下,正则表达式是贪婪匹配的,即尽可能多地匹配字符。可以在重复次数后面加上`?`来表示非贪婪匹配。例如,正则表达式`a+?`可以匹配尽可能少的字符`a`。 这些只是正则表达式的一些基本语法规则,正则表达式还有更多高级用法和语法规则。希望以上内容对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值