正则表达式:(?! ...) 是什么意思?

发布于 2024-08-03 06:12:51 字数 141 浏览 7 评论 0原文

以下正则表达式查找子字符串 FTW 和 ODP 之间的文本。

/FTW(((?!FTW|ODP).)+)ODP+/

(?!...) 的作用是什么?

The following regex finds text between substrings FTW and ODP.

/FTW(((?!FTW|ODP).)+)ODP+/

What does the (?!...) do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

玩心态 2024-08-10 06:12:51

(?!regex) 是一个 零宽度负向预测 。它将测试当前光标位置和向前的字符,测试它们是否匹配提供的正则表达式,然后将光标返回到它开始的位置。

整个正则表达式:

/
 FTW           # Match Characters 'FTW'
 (             # Start Match Group 1
  (             # Start Match Group 2
   (?!FTW|ODP)   # Ensure next characters are NOT 'FTW' or 'ODP', without matching
   .             # Match one character
  )+            # End Match Group 2, Match One or More times
 )             # End Match Group 1
 OD            # Match characters 'OD'
 P+            # Match 'P' One or More times
/

所以 - 寻找 FTW,然后在寻找 ODP+ 来结束我们的字符串时捕获。另请确保 FTWODP+ 之间的数据不包含 FTWODP

(?!regex) is a zero-width negative lookahead. It will test the characters at the current cursor position and forward, testing that they do NOT match the supplied regex, and then return the cursor back to where it started.

The whole regexp:

/
 FTW           # Match Characters 'FTW'
 (             # Start Match Group 1
  (             # Start Match Group 2
   (?!FTW|ODP)   # Ensure next characters are NOT 'FTW' or 'ODP', without matching
   .             # Match one character
  )+            # End Match Group 2, Match One or More times
 )             # End Match Group 1
 OD            # Match characters 'OD'
 P+            # Match 'P' One or More times
/

So - Hunt for FTW, then capture while looking for ODP+ to end our string. Also ensure that the data between FTW and ODP+ doesn't contain FTW or ODP

风尘浪孓 2024-08-10 06:12:51

来自 perldoc

零宽度负前瞻断言。例如,/foo(?!bar)/ 匹配任何后面没有“bar”的“foo”。但请注意,前瞻和后视不是一回事。您不能将其用于后视。

如果您正在寻找前面没有“foo”的“bar”,则 /(?!foo)bar/ 将不会执行您的操作想。那是因为 (?!foo) 只是说下一个东西不能是“foo”——事实并非如此,它是一个“bar”,所以“foobar" 将匹配。为此,您必须执行类似 /(?!foo)...bar/ 的操作。我们说“like”是因为你的“bar”前面没有三个字符。您可以这样覆盖: /(?:(?!foo)...|^.{0,2})bar/ 。有时直接说仍然更容易:

if (/bar/ && 

来自 perldoc

零宽度负前瞻断言。例如,/foo(?!bar)/ 匹配任何后面没有“bar”的“foo”。但请注意,前瞻和后视不是一回事。您不能将其用于后视。

如果您正在寻找前面没有“foo”的“bar”,则 /(?!foo)bar/ 将不会执行您的操作想。那是因为 (?!foo) 只是说下一个东西不能是“foo”——事实并非如此,它是一个“bar”,所以“foobar" 将匹配。为此,您必须执行类似 /(?!foo)...bar/ 的操作。我们说“like”是因为你的“bar”前面没有三个字符。您可以这样覆盖: /(?:(?!foo)...|^.{0,2})bar/ 。有时直接说仍然更容易:

!~ /foo$/)

From perldoc:

A zero-width negative look-ahead assertion. For example /foo(?!bar)/ matches any occurrence of "foo" that isn't followed by "bar". Note however that look-ahead and look-behind are NOT the same thing. You cannot use this for look-behind.

If you are looking for a "bar" that isn't preceded by a "foo", /(?!foo)bar/ will not do what you want. That's because the (?!foo) is just saying that the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will match. You would have to do something like /(?!foo)...bar/ for that. We say "like" because there's the case of your "bar" not having three characters before it. You could cover that this way: /(?:(?!foo)...|^.{0,2})bar/ . Sometimes it's still easier just to say:

if (/bar/ && 

From perldoc:

A zero-width negative look-ahead assertion. For example /foo(?!bar)/ matches any occurrence of "foo" that isn't followed by "bar". Note however that look-ahead and look-behind are NOT the same thing. You cannot use this for look-behind.

If you are looking for a "bar" that isn't preceded by a "foo", /(?!foo)bar/ will not do what you want. That's because the (?!foo) is just saying that the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will match. You would have to do something like /(?!foo)...bar/ for that. We say "like" because there's the case of your "bar" not having three characters before it. You could cover that this way: /(?:(?!foo)...|^.{0,2})bar/ . Sometimes it's still easier just to say:

!~ /foo$/)
水晶透心 2024-08-10 06:12:51

意思是“后面没有……”。从技术上讲,这就是所谓的负向前瞻,因为您可以查看前面的内容字符串而不捕获它。它是一类零宽度断言,这意味着此类表达式不会捕获表达式的任何部分。

It means "not followed by...". Technically this is what's called a negative lookahead in that you can peek at what's ahead in the string without capturing it. It is a class of zero-width assertion, meaning that such expressions don't capture any part of the expression.

以为你会在 2024-08-10 06:12:51

正则表达式

/FTW(((?!FTW|ODP).)+)ODP+/

匹配第一个 FTW,紧接着既不是 FTW,也不是 ODP,然后是第一个 ODP 之前的所有后续字符(但如果其中某处有 FTW,则不会匹配),然后是后面的所有字母 P。

因此,在字符串中:

FTWFTWODPFTWjjFTWjjODPPPPjjODPPPjjj

它将匹配粗体部分

FTWFTWODPFTWjjFTWjjODPPPPjjODPPPjjj

Regex

/FTW(((?!FTW|ODP).)+)ODP+/

matches first FTW immediately followed neither by FTW nor by ODP, then all following chars up to the first ODP (but if there is FTW somewhere in them there will be no match) then all the letters P that follow.

So in the string:

FTWFTWODPFTWjjFTWjjODPPPPjjODPPPjjj

it will match the bold part

FTWFTWODPFTWjjFTWjjODPPPPjjODPPPjjj

傲性难收 2024-08-10 06:12:51

'?!'实际上是“(?! ... )”的一部分,这意味着里面的任何内容在该位置都不能匹配。

'?!' is actually part of '(?! ... )', it means whatever is inside must NOT match at that location.

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