你好,我有這樣的正則表達式中的Javascript:正則表達式的Java
var urlregex = new RegExp("((www.)(([a-zA-Z0-9-]){2,}\.){1,4}([a-zA-Z]){2,6}(\/([a-zA-Z-_\/\.0-9#:?=&;,]*)?)?)");
,當我嘗試把它放在一個Java字符串我有這樣的錯誤:
Description Resource Path Location Type
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\) CreateActivity.java /SupLink/src/com/supinfo/suplink/activities line 43 Java Problem
所以我只是想知道我必須改變,以使其在Java中,爲了做到這一點(此功能運行正常):
private boolean IsMatch(String s, String pattern)
{
try {
Pattern patt = Pattern.compile(pattern);
Matcher matcher = patt.matcher(s);
return matcher.matches();
} catch (PatternSyntaxException e){
return false;
}
}
編輯:
感謝你的幫助,現在我有這樣的:
private String regex = "((www.)(([a-zA-Z0-9-]){2,}\\.){1,4}([a-zA-Z]){2,6}(\\/([a-zA-Z-_\\/\\.0-9#:?=&;,]*)?)?)";
但我不匹配我真正想要的(正則表達式是可怕的^^),我想匹配THES類型的網址:
- www.something.com
- something.com
- something.com/anotherthing/anything
你能幫助我嗎?
真的感謝
有很多字符需要用Java中的反斜槓進行轉義。您還需要在Java正則表達式中使用兩個反斜槓:一個用於轉義正則表達式中的字符,另一個用於轉義字符串中的反斜槓。 – jahroy 2013-04-09 18:50:59