gpt4 book ai didi

javascript - WhatsApp 聊天日志的正则表达式

转载 作者:行者123 更新时间:2023-11-30 11:09:26 26 4
gpt4 key购买 nike

我想以数组形式返回 WhatsApp 对话。为此,我使用了正则表达式,但是我无法让它工作。

这是通过 WhatsApp 导出的示例聊天(已编辑):

6/13/18, 3:40 AM - Messages to this group are now secured with end-to-end encryption. Tap for more info.
6/13/18, 3:40 AM - You created group "Test Group"
6/13/18, 3:42 AM - Zack added Emma
6/13/18, 4:06 AM - Zack added Json
6/13/18, 2:35 PM - Zack: Let's meet tomorrow.
6/15/18, 5:34 PM - Emma: I'll create the Discord server by tonight.
We'll look into making the parser.
7/15/18, 12:05 PM - Zack: Great, I'll add that to our schedule.
7/15/18, 12:05 PM - Json: On our team calander - TCal?
7/15/18, 12:05 PM - Zack: Yes, added on 7/15/18, 12:05 PM.
7/15/18, 12:05 PM - Emma: Are we going JS on this?
7/15/18, 12:05 PM - Json: You bet.
7/15/18, 12:05 PM - Zack: JS is love, JS is life.
7/15/18, 1:46 PM - Emma: Haha.
7/15/18, 4:53 PM - Json: 👍🏻

我尝试过以下方法:

/\d{1,2}\/\d{1,2}\/\d{2},\s\d{1,2}:\d{2}\s[AP]M\s-.+\n?/g

正如您所期望的,我得到的内容如下: -

...但如果消息超过 1 行,则会被跳过。

例如

6/15/18, 5:34 PM - Emma: I'll create the Discord server by tonight.
We'll look into making the parser.

摘录:

6/15/18, 5:34 PM - Emma: I'll create the Discord server by tonight.

但我希望它提取为:

Emma: I'll create the Discord server by tonight.
We'll look into making the parser.

最佳答案

您可以在正向前瞻中使用您的(稍微缩短的)模式:

s.split(/(?=^\d{1,2}\/\d{1,2}\/\d{2},\s\d{1,2}:\d{2}\s[AP]M)/m).filter(Boolean)

请参阅regex demo

这里,模式将匹配紧随其后的每行起始位置 \d{1,2}\/\d{1,2}\/\d{2},\s\d {1,2}:\d{2}\s[AP]M) 模式。

JS 演示:

var s = "6/13/18, 3:40 AM - Messages to this group are now secured with end-to-end encryption. Tap for more info.\r\n6/13/18, 3:40 AM - You created group \"Test Group\"\r\n6/13/18, 3:42 AM - Zack added Emma\r\n6/13/18, 4:06 AM - Zack added Json\r\n6/13/18, 2:35 PM - Zack: Let's meet tomorrow.\r\n6/15/18, 5:34 PM - Emma: I'll create the Discord server by tonight.\r\nWe'll look into making the parser.\r\n7/15/18, 12:05 PM - Zack: Great, I'll add that to our schedule.\r\n7/15/18, 12:05 PM - Json: On our team calander - TCal?\r\n7/15/18, 12:05 PM - Zack: Yes, added on 7/15/18, 12:05 PM.\r\n7/15/18, 12:05 PM - Emma: Are we going JS on this?\r\n7/15/18, 12:05 PM - Json: You bet.\r\n7/15/18, 12:05 PM - Zack: JS is love, JS is life.\r\n7/15/18, 1:46 PM - Emma: Haha.\r\n7/15/18, 4:53 PM - Json: 👍🏻";
console.log(s.split(/(?=^\d{1,2}\/\d{1,2}\/\d{2},\s\d{1,2}:\d{2}\s[AP]M)/m).filter(Boolean));

如果添加 .map(x => x.trim()) (或 .map(function(x) { return x. trim ();}))。

关于javascript - WhatsApp 聊天日志的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54341845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com