복붙노트

[PYTHON] 큰 문서에서 전자 메일 하위 문자열 추출

PYTHON

큰 문서에서 전자 메일 하위 문자열 추출

이메일 주소가 수십만 개에 이르는 매우 큰 .txt 파일이 있습니다. 모두 다음 형식을 취합니다.

...<name@domain.com>...

특정 @domain 문자열의 모든 인스턴스를 찾는 전체 .txt 파일을 파이썬이 순환하도록하는 가장 좋은 방법은 무엇입니까? <...>의 주소 전체를 잡고 추가하십시오. 목록? 내가 가진 문제는 다양한 주소의 가변 길이입니다.

해결법

  1. ==============================

    1.이 코드는 문자열의 전자 메일 주소를 추출합니다. 한 줄씩 읽는 동안 사용하십시오.

    이 코드는 문자열의 전자 메일 주소를 추출합니다. 한 줄씩 읽는 동안 사용하십시오.

    >>> import re
    >>> line = "should we use regex more often? let me know at  321dsasdsa@dasdsa.com.lol"
    >>> match = re.search(r'[\w\.-]+@[\w\.-]+', line)
    >>> match.group(0)
    '321dsasdsa@dasdsa.com.lol'
    

    이메일 주소가 여러 개인 경우 findall을 사용하십시오.

    >>> line = "should we use regex more often? let me know at  321dsasdsa@dasdsa.com.lol"
    >>> match = re.findall(r'[\w\.-]+@[\w\.-]+', line)
    >>> match
    ['321dsasdsa@dasdsa.com.lol', 'dadaads@dsdds.com']
    

    위의 정규 표현식은 아마도 가장 일반적인 가짜 전자 메일 주소를 찾습니다. RFC 5322와 완전히 일치하려면 어떤 전자 메일 주소가 사양을 따르는 지 확인해야합니다. 이메일 주소를 올바르게 찾는 데 걸리는 버그를 피하려면이 항목을 확인하십시오.

    편집 : @ kostek의 의견에 제안 된대로 : 문자열 support@example.com에 문의하십시오. 내 정규 표현식은 support@example.com을 반환합니다. (끝에 점). 이것을 피하려면 [\ w \.,] + @ [\ w \.,] + \. \ w +)를 사용하십시오.

    편집 II : 또 다른 멋진 개선점이 코멘트에 언급되었습니다 : [\ w \ .-] + @ [\ w \ .-] + \. \ w + example@do-main.com도 캡처합니다.

  2. ==============================

    2.

    import re
    line = "why people don't know what regex are? let me know asdfal2@als.com, Users1@gmail.de " \
           "Dariush@dasd-asasdsa.com.lo,Dariush.lastName@someDomain.com"
    match = re.findall(r'[\w\.-]+@[\w\.-]+', line)
    for i in match:
        print(i)
    

    목록에 추가하려면 "일치"

    print(match)
    

    희망이 도움이됩니다.

  3. ==============================

    3.특정 도메인을 찾고있는 경우 :

    특정 도메인을 찾고있는 경우 :

    >>> import re
    >>> text = "this is an email la@test.com, it will be matched, x@y.com will not, and test@test.com will"
    >>> match = re.findall(r'[\w-\._\+%]+@test\.com',text) # replace test\.com with the domain you're looking for, adding a backslash before periods
    >>> match
    ['la@test.com', 'test@test.com']
    
  4. ==============================

    4.

    import re
    rgx = r'(?:\.?)([\w\-_+#~!$&\'\.]+(?<!\.)(@|[ ]?\(?[ ]?(at|AT)[ ]?\)?[ ]?)(?<!\.)[\w]+[\w\-\.]*\.[a-zA-Z-]{2,3})(?:[^\w])'
    matches = re.findall(rgx, text)
    get_first_group = lambda y: list(map(lambda x: x[0], y))
    emails = get_first_group(matches)
    

    제발이 악명 높은 정규 표현식에 대해 가서 나를 미워하지 마세요. 정규 표현식은 아래에 표시된 이메일 주소의 일부분에서 작동합니다. 나는 주로 이것을 이메일 주소의 유효한 문자에 대한 근거로 사용했다.

    부담없이 여기에서 놀아 라.

    정규식이 example.com의 이름과 같은 전자 메일을 캡처하는 변형도 만들었습니다.

    (?:\.?)([\w\-_+#~!$&\'\.]+(?<!\.)(@|[ ]\(?[ ]?(at|AT)[ ]?\)?[ ])(?<!\.)[\w]+[\w\-\.]*\.[a-zA-Z-]{2,3})(?:[^\w])
    
  5. ==============================

    5.emailregex.com의 정규식을 사용하여이 특정 문제에 대한 또 다른 접근법을 제시합니다.

    emailregex.com의 정규식을 사용하여이 특정 문제에 대한 또 다른 접근법을 제시합니다.

    text = "blabla <hello@world.com>><123@123.at> <huhu@fake> bla bla <myname@some-domain.pt>"
    
    # 1. find all potential email addresses (note: < inside <> is a problem)
    matches = re.findall('<\S+?>', text)  # ['<hello@world.com>', '<123@123.at>', '<huhu@fake>', '<myname@somedomain.edu>']
    
    # 2. apply email regex pattern to string inside <>
    emails = [ x[1:-1] for x in matches if re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", x[1:-1]) ]
    print emails   # ['hello@world.com', '123@123.at', 'myname@some-domain.pt']
    
  6. from https://stackoverflow.com/questions/17681670/extract-email-sub-strings-from-large-document by cc-by-sa and MIT license