简体   繁体   中英

How to match a long String with a regex fast?

I have this regex "((\\\\-)?[0-9]+(.([0-9])+)? )+" that should match sequence of numbers each separated by a single space. For example "5 4 1 2 2.4 3 7.8" or "5 4 1 2 2.4 8.001 7.8".

In order to check if the string matches the regex I do:

if((value+" ").matches("((\\-)?[0-9]+(.([0-9])+)? )+")){
    // anything
}

The thing is, when I give this small string like the examples above, it goes perfectly. But for longer strings like: "2000000 2000000 2000000 2000000 2000000 2000000 2000000 2000000" it goes perfectly if matches, but takes up to 5 seconds if doesn't match. Check this:

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());

This takes up to 5 seconds!! while if you removed the " h" from the end of the string, it would take less than 1 ms.

Any ideas?

我想,如果你拆分到上述数字序列(由空格分开),然后将一个简单的正则表达式的每个子你会得到更快的性能。

First you need fix your regex:

"((\\-)?[0-9]+(\\.([0-9])+)? )+"

because your version match any symbol between two numbers including the space. Maybe this makes performance down.

After that you can first try to find any character and if found, do not check with your regex or splitting to smaller pieces as someone tell before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM