2013-09-27 46 views
1

我想從以下方法得到爲什麼<[^>] *>不工作的正則表達式

<[email protected]> 

asdasdasdasdasdadasdsad<[email protected]>asdasdasdasdasd 

private String extractInvalidEmailAddress(String message) { 
    Pattern pattern = Pattern.compile("<[^>]*>"); 
    Matcher matcher = pattern.matcher(message); 
    matcher.find(); 
    message = matcher.group(); 

    return message; 
} 

但是,所有我得到是null!我需要做些什麼來獲得[email protected]

+4

它應該肯定工作正常。你能展示你如何調用方法,並使用返回值嗎? –

+0

因爲這是一個笑臉。 –

+0

System.out.println(extractTicketEventId(extractInvalidEmailAddress(「asdasdasdasdasdadasdsad <[email protected]> asdasdasdasdasd」))); – Ikthiander

回答

0

試試這個:

Pattern pattern = Pattern.compile("<.*?>"); 
+0

即使這不是工作的人,最近怎麼樣?它在正則表達式測試器中運行良好... – Ikthiander

2

它應該有工作。你可以試試這個1個襯墊方法,做同樣的工作,爲您的較長的代碼:

private String extractInvalidEmailAddress(String message) { 
    return message.replaceAll("^.*?(<[^>]*>).*$", "$1"); 
} 
+0

它可能是一行,但它的可讀性差得多。 – Dukeling

+0

使您的代碼1行不一定是件好事:可讀性與功能性 –

0

正如其他人所說,這種方法是正確的。您似乎將一個空值傳遞給此方法。我將設置一個調試點來了解您的應用程序中發生了什麼。

相關問題