2013-05-02 3 views
1

나는 모든 이메일 주소를 탐지하는 정규 표현식을 가지고있다. 전자 메일 주소를 세는 전자 메일 메시지의 헤더에서 특별히 보이는 정규 표현식을 만들고 특정 도메인 (abc.com)의 전자 메일 주소를 무시하려고한다.정규식은 전자 메일 헤더의 전자 메일 주소 수를 감지합니까?

예를 들어, [email protected]의 10 개의 이메일 주소가 [email protected]의 11 번째 주소를 무시합니다.

현재 정규식 :.

^[A-Z0-9 ._ % + -] + @ [A-Z0-9 .-] + [AZ] {2,4} $

+0

당신이 사용하는 어떤 언어 뒤에 문자 덩어리를 찾고 계속? –

+6

당신은 RFC에 따라 모든 유효한 전자 메일 주소를 다루는 정규 표현식을 생성 할 수 없다는 것을 알고 있습니까? –

+3

정규식을 사용하지 마십시오. 본격적인 전자 메일 헤더 파서를 사용하고 원하는 도메인을 필터링하고 (필요한 경우 정규식을 사용하여) 결과를 계산합니다. – Bergi

답변

1

보편적 인 정규식의 다음 powershell 예제를 생각해보십시오.

모든 이메일 주소를 찾으려면 : 헤더에 모든 이메일 주소는 대괄호가없는 경우 서버가 괄호
  • (?<!Content-Type(.|\n){0,10000000})([a-zA-Z0-9.!#$%&''*+-/=?\^_``{|}~-][email protected](?!abc.com)[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)와 이메일 주소를 둘러싼 경우

    • <(.*?)>이 편리합니다. 이 특정 정규식은 stackoverflow 201323의 커뮤니티 위키 답변에서 복사되었으며 @abc.com을 방지하기 위해 여기에서 수정되었습니다. 아마도이 정규식이 작동하지 않을 몇 가지 가장자리 경우가 있습니다. 따라서 동일한 페이지에는 모든 전자 메일 주소와 일치하는 복잡한 정규식이 있습니다. 나는 @abc.com을 건너 뛰도록 수정할 시간이 없다.

    $Matches = @() 
        $String = 'Return-Path: <[email protected]> 
    X-SpamCatcher-Score: 1 [X] 
    Received: from [136.167.40.119] (HELO abc.com) 
        by fe3.abc.com (CommuniGate Pro SMTP 4.1.8) 
        with ESMTP-TLS id 61258719 for [email protected]; 
    Message-ID: <[email protected]> 
    Date: Wed, 21 Jan 2009 12:52:00 -0500 (EST) 
    From: Taylor Evans <[email protected]> 
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.1) 
    X-Accept-Language: en-us, en 
    MIME-Version: 1.0 
    To: Jon Smith <[email protected]> 
    Subject: Business Development Meeting 
    Content-Type: text/plain; charset=us-ascii; format=flowed 
    Content-Transfer-Encoding: 7bit 
    Content-Type: multipart/alternative; 
    boundary="------------060102080402030702040100" 
    This is a multi-part message in MIME format. 
    --------------060102080402030702040100 
    Content-Type: text/plain; charset=ISO-8859-15; format=flowed 
    Content-Transfer-Encoding: 7bit 
    Hello, 
    this is an HTML mail, it has *bold*, /italic /and _underlined_ text. 
    And then we have a table here: 
    Cell(1,1) 
    Cell(2,1) 
    Cell(1,2) Cell(2,2) 
    And we put a picture here: 
    Image Alt Text 
    That''s it. 
    --------------060102080402030702040100 
    Content-Type: multipart/related; 
    boundary="------------030904080004010009060206" 
    --------------030904080004010009060206 
    Content-Type: text/html; charset=ISO-8859-15 
    Content-Transfer-Encoding: 7bit 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
    <head> 
    <meta http-equiv="content-type" content="text/html; 
    charset=ISO-8859-15"> 
    </head> 
    <body bgcolor="#ffffff" text="#000000"> 
    Hello,<br> 
    <br> 
    this is an HTML mail, it has <b>bold</b>, <i>italic </i>and <u>underlined</u> 
    text.<br> 
    And then we have a table here:<br> 
    <table border="1" cellpadding="2" cellspacing="2" height="62" 
    width="401"> 
    <tbody> 
    <tr> 
    <td valign="top">Cell(1,1)<br> 
    </td> 
    <td valign="top">Cell(2,1)</td> 
    </tr> 
    <tr> 
    <td valign="top">Cell(1,2)</td> 
    <td valign="top">Cell(2,2)</td> 
    </tr> 
    </tbody> 
    </table> 
    <br> 
    And we put a picture here:<br> 
    <br> 
    <img alt="Image Alt Text" 
    src="cid:[email protected]" height="79" 
    width="98"><br> 
    <br> 
    That''s it. email me at [email protected]<br> 
    Subject: <br> 
    </body> 
    </html>' 
    
        # Write-Host start with 
    # write-host $String 
    Write-Host 
    Write-Host found 
    [array]$Found = ([regex]'(?<!Content-Type(.|\n){0,10000000})([a-zA-Z0-9.!#$%&''*+-/=?\^_`{|}~-][email protected](?!abc.com)[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)').matches($String) 
    
    $Found | foreach { 
        write-host "key at $($_.Groups[1].Index) = '$($_.Groups[1].Value)'" 
        } # next match 
    Write-Host "found $($Found.count) matching addresses" 
    

    • (?<!Content-Type(.|\n){0,10000000}) 이메일 주소 전에 10,000,000 자 이내로 나타나지 Content-Type을 방지
      found 
      key at 14 = '[email protected]' 
      key at 200 = '[email protected]' 
      key at 331 = '[email protected]' 
      key at 485 = '[email protected]' 
      found 4 matching addresses 
      

      개요를 얻을 수 있습니다. 이것은 메시지 본문에있는 전자 메일 주소 일치를 방지하는 효과가 있습니다. 요청자가 Java를 사용하고 Java가 lookbehind 내에서 *을 사용하는 것을 지원하지 않기 때문에 대신 {0,10000000}을 사용하고 있습니다. (Regex look behind without obvious maximum length in Java 참조). 예상대로 캡처되지 않을 수있는 엣지 케이스가있을 수 있습니다.

    • <(.*[email protected](?!abc.com).*?)>
      • ( 시작 반환
      • [a-zA-Z0-9.!#$%&''*+-/=?\^_``{|}~-]+ 일치 1 이상 허용되는 문자. 이중 따옴표는 powershell의 작은 따옴표 문자를 이스케이프하는 것입니다. 그리고 double back tick은 stackoverflow를 위해 백틱을 이스케이프합니다.
      • @
      • abc.com
      • [a-zA-Z0-9-]+ 첫 번째 점이나 문자열의 끝 개까지 비 욕심 나머지 모든 문자를 찾고 계속 포함하면 찾을 기호
      • (?!abc.com)에서 처음 거부 등이 있습니다.
      • (?:\.[a-zA-Z0-9-]+)*)는 점
  • +0

    간단한 단일 .string.find 줄에서이 작업을 수행하려고합니다. 도움이 될만한 전자 메일 주소의 머리글을 찾아 낼 수만 있다면 – user1181862

    +0

    논리가없고 한 줄 정규식을 허용하는 대답이 업데이트되었습니다. –

    +0

    많은 고마워 ... 시험해 볼거야. – user1181862