相关疑难解决方法(0)

Java的子串()的时间复杂度

String#substring()Java中方法的时间复杂度是多少?

java substring time-complexity

77
推荐指数
4
解决办法
5万
查看次数

Java正则表达式性能

我正在尝试使用Java解析带有正则表达式的链接.

但我认为它变得太慢了.例如,要从以下位置提取所有链接:

......花了34642毫秒(34秒!!!)

这是正则表达式:

private final String regexp = "<a.*?\\shref\\s*=\\s*([\\\"\\']*)(.*?)([\\\"\\'\\s].*?>|>)";
Run Code Online (Sandbox Code Playgroud)

模式的标志:

private static final int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL |Pattern.MULTILINE | Pattern.UNICODE_CASE | Pattern.CANON_EQ;
Run Code Online (Sandbox Code Playgroud)

代码可能是这样的:

private void processURL(URL url){
    URLConnection connection;
    Pattern pattern = Pattern.compile(regexp, flags);
    try {
        connection = url.openConnection();
        InputStream in = connection.getInputStream();
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        String html = new String();
        String line = bf.readLine();            
        while(line!=null){
            html += line;
            line = bf.readLine();
        }
        bf.close();
        Matcher matcher = pattern.matcher(html);
        while (matcher.find()) {
            System.out.println(matcher.group(2));
        } …
Run Code Online (Sandbox Code Playgroud)

java regex performance benchmarking profiling

3
推荐指数
1
解决办法
1万
查看次数