假設我有一個字符串kk a.b.cjkmkc jjkocc a.b.c.
查找使用正則表達式
我想找到字符串中的子a.b.c
在字符串中subtring,但它不工作。
這裏是我的代碼
Pattern p = Pattern.compile("a.b.c");
Matcher m = p.matcher(str);
int x = m.find()
假設我有一個字符串kk a.b.cjkmkc jjkocc a.b.c.
查找使用正則表達式
我想找到字符串中的子a.b.c
在字符串中subtring,但它不工作。
這裏是我的代碼
Pattern p = Pattern.compile("a.b.c");
Matcher m = p.matcher(str);
int x = m.find()
正如其他人所說,.
是正則表達式中一個特殊的系統字符。您可以讓Java使用Pattern.quote引用特殊字符。順便說一下:String.indexof(String)(哪個更快)。如果你真的需要正則表達式,看看這個:
String str = "kk a.b.cjkmkc jjkocc a.b.c.";
Pattern p = Pattern.compile(Pattern.quote("a.b.c"));
Matcher m = p.matcher(str);
while (m.find()) {
int x = m.start();
// ...
}
在一個正則表達式中'''做什麼? –
爲什麼不簡單地使用String.indexOf()? – taytay