Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't find regex pattern to extract authors and messages of WhatsApp chat history

I am currently working on a WhatsApp chat analyser and I have been trying to figure out the pattern for the authors and messages of the chat history but I am not successful.

I have a sample of the chat which looks like this:

08.03.22, 20:55 - Laura: Ja klingt gut :)
08.03.22, 21:00 - Anil: Wunderbar :)

What is the pattern, that could extract Laura , Anil into one list and Ja klingt gut :) , Wunderbar :) into another. For the dates and times I already found the pattern.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance.

>Solution :

If your messages are in a list.

v = ["08.03.22, 20:55 - Laura: Ja klingt gut :)",
     "08.03.22, 21:00 - Anil: Wunderbar :)"]

import re
pattern = re.compile(r'- ([A-Za-z]*):([A-Za-z :)]*)')
names = [pattern.findall(x)[0][0] for x in v] 
messages = [pattern.findall(x)[0][1] for x in v] 

You can try the above code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading