2016-08-17 104 views

回答

0

模式的識別URL(基於關閉RFC 3986)

private static final Pattern urlPattern = Pattern.compile(
     "(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)" 
       + "(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*" 
       + "[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*[email protected]!:/{};']*)", 
     Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); 


//Usage: email content goes as input here.. 
Matcher matcher = urlPattern.matcher("foo bar http://example.com baz"); 

while (matcher.find()) { 
    int matchOffsetStart = matcher.start(1); 
    int matchOffsetEnd = matcher.end(); 
    // now you have the offsets of a URL match 
} 

UPDATE: 如果你想讀像等消息頭,我會建議使用Mime4J

Message msg = new Message(new FileInputStream("mime.msg")); 

msg.getFrom()會給你從。同樣,你可以提取任何你想要的。

Refer for more details here

+0

謝謝Sunil的模式,在此之前,我需要先等待並驗證電子郵件,而不是解析該電子郵件的內容。 – Aminul

+0

驗證電子郵件 - 你的意思是發送人的權利?我沒有完全得到你 –

+0

我的步驟應該是 - 1.需要檢查預期的電子郵件是否已發送,2.然後我將解析網址並繼續。 – Aminul