2013-04-17 41 views
11

我的意圖是從網頁上獲取電子郵件地址。我有頁面源。我正在逐行閱讀頁面源代碼。現在我想從我正在閱讀的當前行獲取電子郵件地址。該當前行可能有或沒有電子郵件。我看到了很多正則表達式的例子。但其中大多數是用於驗證電子郵件地址。我想從頁面源中獲取未驗證的電子郵件地址。作爲http://emailx.discoveryvip.com/工作正則表達式來查找字符串的電子郵件地址

它應該工作的一些例子輸入線路有:

1)<p>Send details to <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;%72%65%62%65%6b%61%[email protected]%68%61%63%6b%73%75%72%66%65%72.%63%6f%6d">[email protected]</a></p> 

2)<p>Interested should send details directly to <a href="http://www.abcdef.com/abcdef/">www.abcdef.com/abcdef/</a>. Should you have any questions, please email <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6a%6f%62%[email protected]%72%65%6c%61%79.%65%64%75">[email protected]</a>. 

3)Note :- Send your queries at [email protected] for more details call Mr. neeraj. 

我想從實例1,2和3 我用java獲得[email protected],我不擅長rexexp。幫我。

+1

你檢查什麼谷歌說,關於「java的正則表達式的電子郵件」? – Vitaly

+0

檢查http://emailx.discoveryvip.com/的頁面源。他們已經提供了提取電子郵件的方法。但我想要一個Java版本 – Neeraj

+2

你有什麼嘗試?堆棧溢出是一個問答網站,而不是「爲我工作」網站。告訴我們你有什麼,所以我們可以幫助你解決你的具體問題。 –

回答

10

您可以驗證電子郵件地址格式爲根據RFC 2822,與此:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]) 

,這裏是從regular-expressions.info解釋:

此正則表達式有兩個部分: @之前的部分和@之後的部分。 @之前的部分有兩種選擇:它可以由一系列字母,數字和某些符號組成,包括一個或多個點。但是,點可能不會連續出現或者在電子郵件地址的開始或結尾處出現。另一種替代方法要求將@之前的部分用雙引號括起來,以允許引號之間的任何ASCII字符串。空白字符,雙引號和反斜槓必須用反斜槓進行轉義。

你可以看看這裏:Rubular example

+0

非常感謝NomNomBot。我正在嘗試與您的正則表達式。 – Neeraj

+0

確保逃脫正斜槓,反斜槓等。 – 2013-04-17 07:41:45

+0

是的。完成。並正常工作。謝謝。 – Neeraj

2

你需要這樣的正則表達式:

".*(\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b).*" 

如果匹配,你可以提取第一組,這將是您的電子郵件。

String regex = ".*(\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\b).*"; 
Pattern p = Pattern.compile(regex); 
Matcher m = p.matcher("your text here"); 
if (m.matches()) { 
    String email = m.group(1); 
    //do somethinfg with your email 
} 
+0

文中有很多電子郵件 – Vitaly

+0

如何獲得第一個匹配的文本 – Stunner

+0

@Petar Ivanov它不適合我。 –

13

正確的代碼

Pattern p = Pattern.compile("\\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{2,4}\\b", 
    Pattern.CASE_INSENSITIVE); 
Matcher matcher = p.matcher(input); 
Set<String> emails = new HashSet<String>(); 
while(matcher.find()) { 
    emails.add(matcher.group()); 
} 

這會給你長的text/html輸入郵件地址的列表。

+1

這不包括具有兩個以上部分的域名,例如在英國,您的地址類似[email protected]。此外,您現在還有一堆長度超過4個字符的新頂級域名。 –

1

這是一個簡單的方法使用Patterns.EMAIL_ADDRESS來提取輸入字符串的所有電子郵件:

public static List<String> getEmails(@NonNull String input) { 
     List<String> emails = new ArrayList<>(); 
     Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(input); 
     while (matcher.find()) { 
      int matchStart = matcher.start(0); 
      int matchEnd = matcher.end(0); 
      emails.add(input.substring(matchStart, matchEnd)); 
     } 
     return emails; 
    } 
相關問題