问题描述
我有一个聊天记录,我希望每个组的类型为(Stranger|You): message
。这是聊天记录的格式:
foofoofoofoofoofooStranger: heyy You: asdasdasdassdasad Stranger: asdasdasd You: Stranger:asdasdasd You: bye You have disconnected.\n\n \n\n \n\x0c
我尝试了(Stranger:\s|You:\s)(.*?)(Stranger:\s|You:\s)
,但效果不佳。
解决方法
使用
((?:Stranger|You):\s+)((?:(?!(?:Stranger|You):\s).)*)
请参见proof
EXPLANATION
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
(?: group,but do not capture:
--------------------------------------------------------------------------------
Stranger 'Stranger'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
You 'You'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\s+ whitespace (\n,\r,\t,\f,and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
(?: group,but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
(?: group,but do not capture:
--------------------------------------------------------------------------------
Stranger 'Stranger'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
You 'You'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\s whitespace (\n,and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \2
,
您可以将最后一个捕获组更改为正向(?=
。
要同时匹配最后一部分,可以添加$
来声明字符串的结尾。
(Stranger:\s|You:\s)(.*?)(?=Stranger:\s|You:\s|$)