What is the RegEx pattern for 24-06-2015 10:15:45: Aditya Krishnakant:?
Question: What is the RegEx pattern for `24-06-2015 10:15:45: Aditya Krishnakant:`

If you look at the whatsapp chat transcript, it looks like a mess. The purpose
of this code is to print messages sent by a person in a new line (for better
readability). This is my code

    
    
    import re
    
    f = open("wa_chat.txt", "r")
    match = re.findall(r'(\d{2})\:(\d{2})\:(\d{4})\s(\d{2})\:(\d{2})\:(\d{2})\:\s(\w)\s(\w)\:', f)
    for content in match:
          print(f.readlines(), '\n')
    f.close()
    

I am getting the following error message:

    
    
    Traceback (most recent call last): 
      File "whatsapp.py", line 4, in <module>
       match = re.findall(r'(\d{2})\:(\d{2})\:(\d{4})\s(\d{2})\:(\d{2})\:(\d{2})\:\s(\w)\s(\w)\:', f)
      File "/usr/lib/python2.7/re.py", line 177, in findall
       return_compile(pattern, flags).findall(string)
    TypeError: expected string or buffer
    

Where am I going wrong?


Answer: For some reason you're putting `\:` where `-` should be. Also, instead of `\s`
you can be more specific and just use a space. You can be more specific with
those kinds of things because you know exactly what the format is. Your other
big problem is that you're only using `\w`, which only matches one
alphanumeric character, when you should use `\w+`, matching the whole word.
Lastly, your actual error is coming from the fact that you're passing in a
file object instead of the string containing its contents, i.e. `f.read()`.
Here's some code that should work:

    
    
    import re
    
    f = open("wa_chat.txt", 'r')
    match = re.findall(r'(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2}):(\d{2}): (\w+) (\w+):', f.read())
    print match #or do whatever you want with it
    

Note that `match` will be a list of tuples since you wanted to use grouping.

