正则表达式:检查 `abc`,然后检查 `def`,中间没有 `123`

标签 regex

如何使用 Regex 查找以 abc 开头,以 def 结尾,但中间不包含 123 的每个字符串?

最佳答案

您可以在此处使用Negative Lookahead

^abc(?:(?!123).)*def$

正则表达式

^              # the beginning of the string
abc            # 'abc'
(?:            # group, but do not capture (0 or more times)
 (?!           # look ahead to see if there is not:
  123          # '123'
 )             # end of look-ahead
 .             # any character except \n
)*             # end of grouping
 def           # 'def'
$              # before an optional \n, and the end of the string

关于正则表达式:检查 `abc`,然后检查 `def`,中间没有 `123`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23435397/

相关文章:

python - 捕获多行文本中出现的多个字符串

javascript - 正则表达式在简短的副本中查找网址

python - 使用正则表达式查找格式为 '[number]' 的字符串

Javascript 正则表达式匹配*不*行首

密码的正则表达式 = 一个数字和大小写字母和特殊字符但是!!开头或结尾没有特殊字符

c++ - Boost::Regex 在 C++ 中的用法

Python正则表达式匹配file-date.txt

php - 需要重新格式化 PHP Formmail 脚本中的电话号码条目

python - Pandas RegEx 提取子字符串上的第一个匹配项,传递的项目数错误

regex - 如何在 HTML5 中验证只允许在 'input type="文件中“'JPG、GIF 或 PNG?