我想寫以下正則表達式:如何使用正則表達式從郵寄地址提取信息鏈接
樣式1:
至mailto:[email protected]
regex1
應匹配所有以mailto:
模式2:
的mailto:[email protected]主題=印度&體=你好
的regex2
應(後弦?)提取查詢字符串
我想寫以下正則表達式:如何使用正則表達式從郵寄地址提取信息鏈接
樣式1:
至mailto:[email protected]
regex1
應匹配所有以mailto:
模式2:
的mailto:[email protected]主題=印度&體=你好
的regex2
應(後弦?)提取查詢字符串
無需常用表達。只需匹配前7個字符爲「mailto:」的任何字符串即可。
如果你堅持使用正則表達式,表達式將是「mailto:。*」。如果你只是想保留什麼是後 「的mailto:」,這將是 「電子郵件地址:(。*)」
+1你可以看看正則表達式2 – 2010-02-14 13:44:59
你真的想用正則表達式來拉開一個查詢串?有專門設計的圖書館可以處理所有邊緣案例。 – 2010-02-14 14:33:54
對於第二種情況,請參閱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
對於第一,你可以使用String.startsWith(字符串前綴)
而且其次也許修剪字符串爲「?」然後使用String.split(「& | =」);你甲肝與 [主題,印度,身體,你好]
http://java.sun.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)
正則表達式實在是沒有必要的數組。一個簡單的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);
}
}
/^mailto:(.+?)$/? – dangerstat 2010-02-14 13:41:55
對於Q2,請參閱http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java – kennytm 2010-02-14 14:32:41