2010-02-14 62 views
1

我想寫以下正則表達式:如何使用正則表達式從郵寄地址提取信息鏈接

樣式1:

至mailto:[email protected]

regex1應匹配所有以mailto:

模式2:

的mailto:[email protected]主題=印度&體=你好

regex2應(後弦?)提取查詢字符串

+1

/^mailto:(.+?)$/? – dangerstat 2010-02-14 13:41:55

+0

對於Q2,請參閱http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java – kennytm 2010-02-14 14:32:41

回答

4

無需常用表達。只需匹配前7個字符爲「mailto:」的任何字符串即可。

如果你堅持使用正則表達式,表達式將是「mailto:。*」。如果你只是想保留什麼是後 「的mailto:」,這將是 「電子郵件地址:(。*)」

+0

+1你可以看看正則表達式2 – 2010-02-14 13:44:59

+0

你真的想用正則表達式來拉開一個查詢串?有專門設計的圖書館可以處理所有邊緣案例。 – 2010-02-14 14:33:54

+0

對於第二種情況,請參閱http://stackoverflow.com/questions/2261222/how-to-use-regular-expressions-to-extract-information-from-mailto-links/2261448#2261448 – Daniel 2010-02-14 15:21:35

0
  1. 的mailto:+或至mailto:[^ \ S] +
  2. 的mailto: ??+ \(+)或郵寄地址:?[^ \ S] + \ [^ \ S] +
1

正則表達式實在是沒有必要的數組。一個簡單的String.startsWith可以滿足第一個匹配的要求,您可以使用URL類來提取查詢。

String string = "mailto:[email protected]?subject=Indian&body=hello"; 
if (string.startsWith("mailto:")) { 
    try { 
     URL url = new URL(string); 
     String query = url.getQuery(); // Here is your query string. 

    } catch (MalformedURLException e) { 
     throw new AssertionError("This should not happen: " + e); 
    } 
} 
相關問題