用于聊天消息的正则表达式
Regex for chat messages
我有一个聊天记录,我希望每个群组都是 (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:\s|You:\s)(.*?)(?=Stranger:\s|You:\s|$)
使用
((?:Stranger|You):\s+)((?:(?!(?:Stranger|You):\s).)*)
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: 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
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: 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, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of
我有一个聊天记录,我希望每个群组都是 (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:\s|You:\s)(.*?)(?=Stranger:\s|You:\s|$)
使用
((?:Stranger|You):\s+)((?:(?!(?:Stranger|You):\s).)*)
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: 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
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: 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, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of