【问题标题】:Regex for chat messages聊天消息的正则表达式
【发布时间】:2020-08-11 04:07:09
【问题描述】:

我有一个聊天对话日志,我希望每个组的类型为 (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),但效果不太好。

【问题讨论】:

标签: regex chat


【解决方案1】:

您可以将最后一个捕获组更改为正向前瞻(?=

为了也匹配最后一部分,您可以添加$ 来断言字符串的结尾。

(Stranger:\s|You:\s)(.*?)(?=Stranger:\s|You:\s|$)

Regex demo

【讨论】:

    【解决方案2】:

    使用

    ((?:Stranger|You):\s+)((?:(?!(?:Stranger|You):\s).)*)
    

    proof

    解释

    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, \r, \t, \f, and " ")
    --------------------------------------------------------------------------------
          )                        end of look-ahead
    --------------------------------------------------------------------------------
          .                        any character except \n
    --------------------------------------------------------------------------------
        )*                       end of grouping
    --------------------------------------------------------------------------------
      )                        end of \2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 2020-09-10
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2016-11-26
      • 2019-05-02
      相关资源
      最近更新 更多