How to extract data from the file by using regular expression in python?
Question: I have a file, in that emailid, phoneno and my dateofbirth is there. In python
by using regular expression, how to find the 3 fields one by one? The desired
output looks like

    
    
    Emailid: x@gmail.com
    Phoneno: 1234567890
    dateofbirth: xx-xx-xx
    

I knew how to find the fields individually. But I am not aware how to find 3
at a time .The below snippet shows how to find the emailid from the file. This
code output looks like

    
    
    Emaildid: x@gmail.com
    

# ...........................

    
    
    import sys,re
    
    pattern=r'''(?P<emailid>[a-zA-Z\.]*\@[a-zA-Z]*\.c[a-zA-Z]*)'''
    
    
    regobj = re.compile(pattern, re.VERBOSE)
    
    for line in sys.stdin:
      results= regobj.finditer(line)
    for result in results:
      sys.stdout.write("%s\n"%result.group('emailid'))
    


Answer: You can iterate over over all non-overlapping matches for the RE pattern in
string using the `finditer` method as follows:

    
    
    import sys,re
    
    pattern = re.compile(r'''(?P<emailid>[a-zA-Z.]*@[a-zA-Z]*\.c[a-zA-Z]*).*?(?P<phone>\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}).*?(?P<dob>[0-9]{2}-[0-9]{2}-[0-9]{2})''', re.DOTALL)
    
    for result in pattern.finditer(sys.stdin.read()):
        sys.stdout.write("Emailid: %s\n"%result.group('emailid'))
        sys.stdout.write("Phoneno: %s\n"%result.group('phone'))
        sys.stdout.write("dateofbirth: %s\n"%result.group('dob'))
    

