Regex to match whatsapp chat log

扶醉桌前 提交于 2019-12-06 07:20:58

There you go:

^
(?P<datetime>\d{2}/\d{2}/\d{4}[^-]+)\s+-\s+
(?P<name>[^:]+):\s+
(?P<message>[\s\S]+?)
(?=^\d{2}|\Z)

See your modified demo on regex101.com.


Essentially, I added anchors, simplified your datetime part and inserted a [\s\S]+? which means: match anything lazily (including newlines) up to the following condition which is a lookahead. The lookahead makes sure there's either another two digits right after a newline (could be tightened!) or the very end of the string.

The dot does not match newline characters, which is why you only get the first line matched. The matching behaviour of a regular expression engine can usually be modified with flags.

On the regexp101 page, you can click on Set Regex Options (the flag right next to the regular expression input field) and activate Single line, then the dot will also match \n.

But then you have to modify your expression so that it detects the start of the next message, otherwise everything will be interpreted as one message.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!