这是我的字符串:
$str = "this is a string
this is a test string";
我想匹配 this
和 string
单词(加上它们本身) 之间的所有内容。
注意:这两个词之间可以是除了test
这个词之外的所有内容。
所以我试图匹配this is a string
,而不是this is a test string
。因为第二个包含test
这个词。
这是我目前的模式:
/this[^test]+string/gm
But it doesn't work as expected
我该如何解决?
最佳答案
你这样做的方式是排除列表“测试”中的任何字符。这样做的方法是使用 negative lookarounds
。正则表达式将如下所示。
this((?!test).)*string
关于php - 如果它不包含特定单词,我该如何匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41530528/