2019-07-04: 정규 연습

1818 단어 Python
#encoding=utf-8
"""
 
"""
import re
def getTime(path):
    email=[]
    pattern=re.compile(r"::([A-Z]+)@[A-Z]+(.*)::",re.I)
    with open(path,"r") as fp:
        for line in fp:
            email.append(pattern.search(line).group(1) + ":" + pattern.search(line).group(2))
    return email

"""
 
 ( , )
"""
def getName(path):
    usernames = []
    domains = []
    pattern1 = re.compile(r"::([A-Z]+)@.*::", re.I)
    pattern2 = re.compile(r"::[A-Z]+@[A-Z]+(.*)::", re.I)
    with open(path) as file_obj:
        for line in file_obj:
            usernames.append(pattern1.search(line).group(1))
            domains.append(pattern2.search(line).group(1))
    return usernames,domains

"""
 
"""
def changeEmail(path):
    with open(path,"r") as fp:
        for line in fp:
            newEmail=re.sub("\w+@\w+.\w+","[email protected]",line)
            with open("e:\\murphy\\2.txt","a+") as fp1:
                fp1.write(newEmail)

if __name__=="__main__":
    print(getTime("E:\\murphy\\1.txt"))
    print(getName("E:\\murphy\\1.txt"))
    print(changeEmail("E:\\murphy\\1.txt"))


사용된 데이터
Fri Apr 12 02:04:19 2030::[email protected]::1902161059-7-10 Sun Sep  2 14:33:07 1973::[email protected]::115799587-6-8 Wed Aug 21 05:11:12 2030::[email protected]::1913490672-7-10 Sun Jan  6 08:14:19 1980::[email protected]::315965659-6-7 Sat Mar 29 15:17:51 1975::[email protected]::165309471-5-11 Wed Jul 22 19:47:29 1970::[email protected]::17495249-6-11 Thu May 12 08:08:08 2022::[email protected]::1652314088-6-10 Mon Jul 10 03:27:13 1978::[email protected]::268860433-5-11

좋은 웹페이지 즐겨찾기