尝试开发正则表达式以提取不包含特定单词的句子。为了简单起见,这里是一个简单的示例:
输入: 矢状侦察器图像颈椎:轻度至中度多级脊椎病。存在骨折。
所需的输出: 存在骨折。
尝试#1
正则表达式:
[^.]*(?!cervi(c|x))[^.]*\.
实际输出: 矢状球探影像颈椎:轻度至中度多级脊椎病。存在骨折。
尝试#2:
正则表达式:
[^.]*[^(cervi(c|x))][^.]*\.
实际输出: 矢状球探影像颈椎:轻度至中度多级脊椎病。存在骨折。
中验证这些结果答案 0 :(得分:1)
使用
(?<![^.])\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