3 回答

TA貢獻1865條經驗 獲得超7個贊
你應該考慮使用一些 NLP 包將文本拆分成句子。然后使用
^This\s+\S+\s+\S+\s+on\b
它匹配一個以 wth 開頭的字符串This
,然后有兩個包含任何非空白字符的單詞,然后是單詞on
。
見證明
解釋
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
This 'This'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
on 'on'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char

TA貢獻1815條經驗 獲得超10個贊
最基本的正則表達式是
/\bThis\s+\w+\s+\w+\s+on\b/
這將匹配沒有捕獲。也許您認為的“單詞”字符可能與正則表達式引擎認為的單詞字符不同。

TA貢獻1775條經驗 獲得超8個贊
(?:^|[.;!?]\s+)(\bThis\W*?(\b\w+\b)\W*?(\b\w+\b)\W*on\b)
這樣的事情會起作用嗎?據我了解,您希望句子有四個詞,以“This”開頭,以“on”結尾。
添加回答
舉報