正则表达式前瞻、后瞻和原子组

发布于 2024-09-04 01:38:04 字数 230 浏览 3 评论 0原文

我在我的正则表达式体内发现了这些东西,但我不知道我可以用它们做什么。 有人有例子,以便我可以尝试理解它们是如何工作的吗?

(?=) - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我要还你自由 2024-09-11 01:38:04

示例

给定字符串 foobarbarfoo

bar(?=bar)     finds the 1st bar ("bar" which has "bar" after it)
bar(?!bar)     finds the 2nd bar ("bar" which does not have "bar" after it)
(?<=foo)bar    finds the 1st bar ("bar" which has "foo" before it)
(?<!foo)bar    finds the 2nd bar ("bar" which does not have "foo" before it)

您还可以将它们组合起来:

(?<=foo)bar(?=bar)    finds the 1st bar ("bar" with "foo" before it and "bar" after it)

定义

前瞻正 (?=)

查找表达式 A,其中表达式 B 跟随:

A(?=B)

前瞻负 (? !)

在表达式 B 不跟在后面的地方查找表达式 A:

A(?!B)

在正数后面查找 (?<=)

在表达式 B 前面查找表达式 A:

(?<=B)A

在负数后面查找 (?<=) !)

查找表达式 B 不在前面的表达式 A:

(?<!B)A

原子组 (?>)

原子组退出组并丢弃第一个之后的替代模式 > 组内匹配的模式(回溯被禁用)。

  • 应用于 foots(?>foo|foot)s 将匹配其第一个替代 foo,然后失败为 s > 不立即跟随,并因回溯被禁用而停止

非原子组将允许回溯;如果后续匹配失败,它将回溯并使用替代模式,直到找到整个表达式的匹配或用尽所有可能性。

  • (foo|foot)s 应用于 foots 将:

    1. 匹配其第一个替代方案 foo,然后由于 s 未立即跟随在 foots 中而失败,并回溯到其第二个替代方案;
    2. 匹配其第二个替代 foot,然后成功,因为 s 紧跟在 foots 中,然后停止。

一些资源

在线测试人员

Examples

Given the string foobarbarfoo:

bar(?=bar)     finds the 1st bar ("bar" which has "bar" after it)
bar(?!bar)     finds the 2nd bar ("bar" which does not have "bar" after it)
(?<=foo)bar    finds the 1st bar ("bar" which has "foo" before it)
(?<!foo)bar    finds the 2nd bar ("bar" which does not have "foo" before it)

You can also combine them:

(?<=foo)bar(?=bar)    finds the 1st bar ("bar" with "foo" before it and "bar" after it)

Definitions

Look ahead positive (?=)

Find expression A where expression B follows:

A(?=B)

Look ahead negative (?!)

Find expression A where expression B does not follow:

A(?!B)

Look behind positive (?<=)

Find expression A where expression B precedes:

(?<=B)A

Look behind negative (?<!)

Find expression A where expression B does not precede:

(?<!B)A

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 to foots will match its 1st alternative foo, then fail as s does not immediately follow, and stop as backtracking is disabled

A 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 to foots will:

    1. match its 1st alternative foo, then fail as s does not immediately follow in foots, and backtrack to its 2nd alternative;
    2. match its 2nd alternative foot, then succeed as s immediately follows in foots, and stop.

Some resources

Online testers

把人绕傻吧 2024-09-11 01:38:04

环视是零宽度断言。它们检查正则表达式(当前位置的右侧或左侧 - 基于前面或后面),找到匹配时成功或失败(基于是正还是负)并丢弃匹配的部分。它们不消耗任何字符 - 它们后面的正则表达式的匹配(如果有)将从相同的光标位置开始。

阅读 regular-expression.info 了解更多详细信息。

  • 正向前瞻:

语法:

(?=REGEX_1)REGEX_2

仅当 REGEX_1 匹配时才匹配;匹配 REGEX_1 后,匹配项将被丢弃,并从同一位置开始搜索 REGEX_2。

示例:

(?=[a-z0-9]{4}$)[a-z]{1,2}[0-9]{2,3}

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_2

仅当 REGEX_1 不匹配时才匹配;检查 REGEX_1 后,从同一位置开始搜索 REGEX_2。

示例:

(?!.*\bFWORD\b)\w{10,30}$

先行部分检查字符串中的 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.

  • Positive lookahead:

Syntax:

(?=REGEX_1)REGEX_2

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:

(?=[a-z0-9]{4}$)[a-z]{1,2}[0-9]{2,3}

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.

  • Negative lookahead

Syntax:

(?!REGEX_1)REGEX_2

Match only if REGEX_1 does not match; after checking REGEX_1, the search for REGEX_2 starts at the same position.

example:

(?!.*\bFWORD\b)\w{10,30}$

The look-ahead part checks for the FWORD in the string and fails if it finds it. If it doesn't find FWORD, 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 characters a-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.

  • Atomic groups basically discards/forgets the subsequent tokens in the group once a token matches. Check this page for examples of atomic groups
人间☆小暴躁 2024-09-11 01:38:04

为什么 - 假设您正在玩 wordle,并且输入了“ant”。 (是的,三个字母的单词,这只是一个例子 - chill)

答案返回为空白、黄色、绿色,并且您有一个希望使用正则表达式来搜索的三个字母单词的列表?你会怎么做?

首先,您可以从第三个位置中存在 t 开始:

[a-z]{2}t

我们可以通过注意到我们没有 a

[b-z]{2}t

我们可以通过说搜索必须有 n 来进一步改进。

(?=.*n)[b-z]{2}t

或将其分解;

(?=.*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:

[a-z]{2}t

We could improve by noting that we don't have an a

[b-z]{2}t

We could further improve by saying that the search had to have an n in it.

(?=.*n)[b-z]{2}t

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

枕梦 2024-09-11 01:38:04

快速环顾四周。
如何区分lookahead和lookbehind?
和我一起花 2 分钟游览一下:

(?=) - positive lookahead
(?<=) - positive lookbehind

假设

    A  B  C #in a line

现在,我们问 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:

(?=) - positive lookahead
(?<=) - positive lookbehind

Suppose

    A  B  C #in a line

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.

樱娆 2024-09-11 01:38:04

我使用后向查找模式并使用负向查找来查找缺少的表(nolock)

expression="(?<=DB\.dbo\.)\w+\s+\w+\s+(?!with\(nolock\))"

matches=re.findall(expression,sql)
for match in matches:
    print(match)

I used look behind to find the schema and look ahead negative to find tables missing with(nolock)

expression="(?<=DB\.dbo\.)\w+\s+\w+\s+(?!with\(nolock\))"

matches=re.findall(expression,sql)
for match in matches:
    print(match)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文