gpt4 book ai didi

regex - 使用正则表达式匹配不包含特定单词的句子

转载 作者:行者123 更新时间:2023-12-05 06:11:26 25 4
gpt4 key购买 nike

尝试开发一个正则表达式来提取不包含特定单词的句子。为了简单起见,我在这里举一个简单的例子:

输入:矢状位侦察图像颈胸:轻度至中度多节段脊椎病。存在骨折。

期望的输出:存在骨折。

尝试#1

正则表达式:[^.]*(?!cervi(c|x))[^.]*\.

实际输出:矢状位侦察图像颈胸:轻度至中度多节段脊椎病。存在骨折。

尝试#2:

正则表达式:[^.]*[^(cervi(c|x))][^.]*\.

实际输出:矢状位侦察图像颈胸:轻度至中度多节段脊椎病。存在骨折。

可以在 https://regexr.com/ 中验证这些结果

最佳答案

使用

(?<![^.])\s*((?:(?!cervi[cx])[^.])*\.)

参见 proof

解释

--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
[^.] any character except: '.'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
cervi 'cervi'
--------------------------------------------------------------------------------
[cx] any character of: 'c', 'x'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[^.] any character except: '.'
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
) end of \1

关于regex - 使用正则表达式匹配不包含特定单词的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63983519/

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