gpt4 book ai didi

regex - 正则表达式 : find string without substring

转载 作者:行者123 更新时间:2023-12-03 11:47:57 25 4
gpt4 key购买 nike

我有一个大文本:

"Big piece of text. This sentence includes 'regexp' word. And this
sentence doesn't include that word"

我需要找到以' 开头的子字符串此 ' 并以 ' 结尾字 ' 但是 没有 包含单词 ' 正则表达式 '。

在这种情况下,字符串:“ this sentence doesn't include that word”正是我想要接收的。

我怎样才能通过正则表达式做到这一点?

最佳答案

使用忽略大小写选项,以下应该有效:

\bthis\b(?:(?!\bregexp\b).)*?\bword\b

示例: http://www.rubular.com/r/g6tYcOy8IT

解释:
\bthis\b           # match the word 'this', \b is for word boundaries
(?: # start group, repeated zero or more times, as few as possible
(?!\bregexp\b) # fail if 'regexp' can be matched (negative lookahead)
. # match any single character
)*? # end group
\bword\b # match 'word'
\b围绕每个单词确保您不匹配子字符串,例如匹配 'thiSTLe' 中的 'this',或匹配 'wordy' 中的 'word'。

这通过检查起始词和结束词之间的每个字符来确保排除的词不会出现。

关于regex - 正则表达式 : find string without substring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11869793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com