正则表达式前瞻、后瞻和原子组
我在我的正则表达式体内发现了这些东西,但我不知道我可以用它们做什么。 有人有例子,以便我可以尝试理解它们是如何工作的吗?
(?=) - positive lookahead
(?!) - negative lookahead
(?<=) - positive lookbehind
(?<!) - negative lookbehind
(?>) - atomic group
I found these things in my regex body but I haven't got a clue what I can use them for.
Does somebody have examples so I can try to understand how they work?
(?=) - positive lookahead
(?!) - negative lookahead
(?<=) - positive lookbehind
(?<!) - negative lookbehind
(?>) - atomic group
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
示例
给定字符串
foobarbarfoo
:您还可以将它们组合起来:
定义
前瞻正
(?=)
查找表达式 A,其中表达式 B 跟随:
前瞻负
(? !)
在表达式 B 不跟在后面的地方查找表达式 A:
在正数后面查找
(?<=)
在表达式 B 前面查找表达式 A:
在负数后面查找
(?<=)
!)查找表达式 B 不在前面的表达式 A:
原子组
(?>)
原子组退出组并丢弃第一个之后的替代模式 > 组内匹配的模式(回溯被禁用)。
foots
的(?>foo|foot)s
将匹配其第一个替代foo
,然后失败为s
> 不立即跟随,并因回溯被禁用而停止非原子组将允许回溯;如果后续匹配失败,它将回溯并使用替代模式,直到找到整个表达式的匹配或用尽所有可能性。
(foo|foot)s
应用于foots
将:foo
,然后由于s
未立即跟随在foots
中而失败,并回溯到其第二个替代方案;foot
,然后成功,因为s
紧跟在foots
中,然后停止。一些资源
在线测试人员
Examples
Given the string
foobarbarfoo
:You can also combine them:
Definitions
Look ahead positive
(?=)
Find expression A where expression B follows:
Look ahead negative
(?!)
Find expression A where expression B does not follow:
Look behind positive
(?<=)
Find expression A where expression B precedes:
Look behind negative
(?<!)
Find expression A where expression B does not precede:
Atomic groups
(?>)
An atomic group exits a group and throws away alternative patterns after the first matched pattern inside the group (backtracking is disabled).
(?>foo|foot)s
applied tofoots
will match its 1st alternativefoo
, then fail ass
does not immediately follow, and stop as backtracking is disabledA non-atomic group will allow backtracking; if subsequent matching ahead fails, it will backtrack and use alternative patterns until a match for the entire expression is found or all possibilities are exhausted.
(foo|foot)s
applied tofoots
will:foo
, then fail ass
does not immediately follow infoots
, and backtrack to its 2nd alternative;foot
, then succeed ass
immediately follows infoots
, and stop.Some resources
Online testers
环视是零宽度断言。它们检查正则表达式(当前位置的右侧或左侧 - 基于前面或后面),找到匹配时成功或失败(基于是正还是负)并丢弃匹配的部分。它们不消耗任何字符 - 它们后面的正则表达式的匹配(如果有)将从相同的光标位置开始。
阅读 regular-expression.info 了解更多详细信息。
语法:
仅当 REGEX_1 匹配时才匹配;匹配 REGEX_1 后,匹配项将被丢弃,并从同一位置开始搜索 REGEX_2。
示例:
REGEX_1 为
[a-z0-9]{4}$
,它匹配四个字母数字字符,后跟行尾。REGEX_2 是
[az]{1,2}[0-9]{2,3}
,匹配一个或两个字母后跟两个或三个数字。REGEX_1 确保字符串的长度确实为 4,但不消耗任何字符,以便从同一位置开始搜索 REGEX_2。现在 REGEX_2 确保该字符串与其他一些规则匹配。如果没有前瞻,它将匹配长度为三或五的字符串。
语法:
仅当 REGEX_1 不匹配时才匹配;检查 REGEX_1 后,从同一位置开始搜索 REGEX_2。
示例:
先行部分检查字符串中的
FWORD
,如果找到则失败。如果找不到FWORD
,则先行查找成功,接下来的部分将验证字符串的长度是否在 10 到 30 之间,并且仅包含单词字符a-zA-Z0- 9_
Look-behind 与look-ahead 类似:它只是查看当前光标位置的后面。某些正则表达式风格(例如 javascript)不支持后向断言。大多数支持它的风格(PHP、Python 等)都要求后视部分具有固定的长度。
Lookarounds are zero width assertions. They check for a regex (towards right or left of the current position - based on ahead or behind), succeeds or fails when a match is found (based on if it is positive or negative) and discards the matched portion. They don't consume any character - the matching for regex following them (if any), will start at the same cursor position.
Read regular-expression.info for more details.
Syntax:
Match only if REGEX_1 matches; after matching REGEX_1, the match is discarded and searching for REGEX_2 starts at the same position.
example:
REGEX_1 is
[a-z0-9]{4}$
which matches four alphanumeric chars followed by end of line.REGEX_2 is
[a-z]{1,2}[0-9]{2,3}
which matches one or two letters followed by two or three digits.REGEX_1 makes sure that the length of string is indeed 4, but doesn't consume any characters so that search for REGEX_2 starts at the same location. Now REGEX_2 makes sure that the string matches some other rules. Without look-ahead it would match strings of length three or five.
Syntax:
Match only if REGEX_1 does not match; after checking REGEX_1, the search for REGEX_2 starts at the same position.
example:
The look-ahead part checks for the
FWORD
in the string and fails if it finds it. If it doesn't findFWORD
, the look-ahead succeeds and the following part verifies that the string's length is between 10 and 30 and that it contains only word charactersa-zA-Z0-9_
Look-behind is similar to look-ahead: it just looks behind the current cursor position. Some regex flavors like javascript doesn't support look-behind assertions. And most flavors that support it (PHP, Python etc) require that look-behind portion to have a fixed length.
为什么 - 假设您正在玩 wordle,并且输入了“ant”。 (是的,三个字母的单词,这只是一个例子 - chill)
答案返回为空白、黄色、绿色,并且您有一个希望使用正则表达式来搜索的三个字母单词的列表?你会怎么做?
首先,您可以从第三个位置中存在 t 开始:
我们可以通过注意到我们没有 a
我们可以通过说搜索必须有 n 来进一步改进。
或将其分解;
(?=.*n) - 向前看,并检查匹配项中是否有 n,它之前可能有零个或多个字符
[bz]{2} - 前两个字母中除了“a”之外的两个字母职位;
t - 字面上是第三个位置的“t”
Why - Suppose you are playing wordle, and you've entered "ant". (Yes three-letter word, it's only an example - chill)
The answer comes back as blank, yellow, green, and you have a list of three letter words you wish to use a regex to search for? How would you do it?
To start off with you could start with the presence of the t in the third position:
We could improve by noting that we don't have an a
We could further improve by saying that the search had to have an n in it.
or to break it down;
(?=.*n) - Look ahead, and check the match has an n in it, it may have zero or more characters before that n
[b-z]{2} - Two letters other than an 'a' in the first two positions;
t - literally a 't' in the third position
快速环顾四周。
如何区分lookahead和lookbehind?
和我一起花 2 分钟游览一下:
假设
现在,我们问 B,你在哪里?
B 声明位置有两种方案:
一、B 前面有 A,后面有 C
二、B 位于 C 的前面(lookahead),而位于 A 的后面(loobehind)。
我们可以看到,两个解中的后面和前面是相反的。
正则表达式是解决方案二。
Grokking lookaround rapidly.
How to distinguish lookahead and lookbehind?
Take 2 minutes tour with me:
Suppose
Now, we ask B, Where are you?
B has two solutions to declare it location:
One, B has A ahead and has C behind
Two, B is ahead (lookahead) of C and behind (loobehind) A.
As we can see, the behind and ahead are opposite in the two solutions.
Regex is solution Two.
我使用后向查找模式并使用负向查找来查找缺少的表(nolock)
I used look behind to find the schema and look ahead negative to find tables missing with(nolock)