regex - 正则表达式:(?!…)是什么意思?

标签 regex negative-lookahead

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

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


(?! ... )是做什么的?

最佳答案

(?!regex)zero-width negative lookahead。它将测试当前光标位置处的字符并向前移动,测试它们与提供的正则表达式不匹配,然后将光标返回到其开始位置。

整个正则表达式:

/
 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 - 正则表达式:(?!…)是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1371149/

相关文章:

javascript - 在 JavaScript 中从 css 规则中提取 RGB 和 RGBA

java - 如何正确使用此 Java 正则表达式的否定先行?

正则表达式:两次比赛之间的否定超前

java - perl 和 java 正则表达式功能之间有什么区别?

c# - 正则表达式: How to escape the "(" meta char in c#

正则表达式负后视和前瞻 : equivalence and performance

regex - 对于这个意外的负先行子模式,我犯了什么错误?

java - 如何创建具有多个条件的正则表达式

java - 禁用字符串转义(反斜杠 hell )

regex - 如何编写搜索模式以在findstr中包含空格?