如何解决正则表达式聊天消息
我有一个聊天记录,我希望每个组的类型为(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|$)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。