我有這個正則表達式"((\\-)?[0-9]+(.([0-9])+)?)+"
應該匹配每個由一個空格分隔的數字序列。例如「5 4 1 2 2.4 3 7.8」或「5 4 1 2 2.4 8.001 7.8」。如何快速匹配長字符串和正則表達式?
爲了檢查字符串我做的正則表達式匹配:
if((value+" ").matches("((\\-)?[0-9]+(.([0-9])+)?)+")){
// anything
}
的事情是,當我給這個小串像上面的例子中,它會很好。但是對於較長的字符串,如:「2000000 2000000 2000000 2000000 2000000 2000000 2000000 2000000」如果匹配,它會很好,但如果不匹配則最多需要5秒。選中此項:
String value = "2000000 2000000 2000000 2000000 2000000 2000000 2000000 2000000 h";
System.out.println("Start: "+System.currentTimeMillis());
if((value+" ").matches("((\\-)?[0-9]+(.([0-9])+)?)+")){
System.out.println("OK");
}else{
System.out.println("NOK");
}
System.out.println("End: "+System.currentTimeMillis());
這需要5秒鐘!而如果從字符串末尾刪除「h」,則需要少於1 ms。
任何想法?
另外我想你可能想逃避點'.'。 – jlordo