我有模式
\\bi\\b[^.!?]{0,10}\\bhate
仅匹配一个句子中的字符串(不除以 .!?
)。但它也与两个单词之间的 not
匹配相反的含义。如何排除最大距离为 10 的两个单词之间出现 not
的情况。
现在它匹配:我不讨厌
。我想排除它,只留下诸如 i do hat
或 i hat
之类的匹配项。
最佳答案
您会在否定断言中排除not
定制长度不超过前 10 个字符。
IE。范围为 10 - length('not')
或 {0,7}
\bi\b(?!.{0,7}不是)[^.!?]{0,10}\bhate
https://regex101.com/r/vdqBQX/1
\b i \b # 'i'
(?! .{0,7} not ) # Here, exclude 'not' if within the first 7 characters
[^.!?]{0,10} \b # O - 10 characters within this negated class
hate
关于java - 将其他两个之间的单词作为正则表达式排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55230260/