您可以使用"\b" + targetword + "\b"
and 这将适用于大多数情况,但是这将匹配单词donald
和字符串O'Donald
或匹配Sarah
字符串Sarah's car is broken
。
我会使用向后看和向前看来验证
- 前面的字符是空格、字符串开头或可接受的标点符号
- 尾随字符是空格、字符串结尾或可接受的标点符号
这些允许的字符可以根据您的实际用例进行定制。
"(?<=[\s",.]|^)" + targetword + "(?=[\s",.\r\n]|$)"
NODE EXPLANATION
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
[\s",.] any character of: whitespace (\n, \r,
\t, \f, and " "), '"', ',', '.'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
targetword 'targetword'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
[\s",.\r\n] any character of: whitespace (\n, \r,
\t, \f, and " "), '"', ',', '.', '\r'
(carriage return), '\n' (newline)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead