您可以使用"\b" + targetword + "\b"
,這將在大多數情況下工作,但是這將字符串Sarah's car is broken
在這個詞donald
,並在串O'Donald
或匹配Sarah
匹配。
我會使用外表屁股,並期待aheads驗證
- 前面的字符是空格,串的開始,或可接受的標點符號
- 尾隨字符是空格,串的端部,或可接受的標點符號
這些允許的字符可根據您的實際使用情況。
"(?<=[\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
我實現了這個解決方案。 這個正則表達式結合我的鏈接功能實際上完全剝離了領先空間,但我設法通過在鏈接創建功能上添加空間來解決這個問題。 –