snippet : find all email addresses in a text file using python
March 4, 2004
import sys
import re
thefile = open(sys.argv[1])
emailRegex = """[a-zA-Z0-9_\-\.]+@""" # id
emailRegex += """(?:""" # choose one of the following
emailRegex += """(?:\[?(?:[0-9]{1,3}\.){1,3}[0-9]{1,3}\]?)""" # IP address
emailRegex += """|"""
emailRegex += """(?:(?:[a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,4})""" #domain name
emailRegex += """)"""
for line in thefile:
for address in re.findall(emailRegex, line):
print address
thefile.close()
Entry Filed under: programming. .





Subscribe to the comments via RSS Feed