正则表达式聊天消息

问题描述

我有一个聊天记录,我希望每个组的类型为(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|$)

Regex demo

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...