3

I'm trying to parse links with regex with Java.

But I think it's getting too slow. For example, to extract all links from:

...it's spending 34642 milliseconds (34 seconds!!!)

Here is the regex:

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

The flags for the pattern:

private static final int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL |Pattern.MULTILINE | Pattern.UNICODE_CASE | Pattern.CANON_EQ;

And the code may be something like this:

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));
        }
     } catch (Exception e){
     }
 }

Can you give me a Hint?

Extra Data:
1Mbit
Core 2 Duo
1Gb RAM
Single Threaded

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
santiagobasulto
  • 11,320
  • 11
  • 64
  • 88
  • 3
    regex in scraping a website! Bad bad bad option! – zengr Oct 11 '10 at 22:55
  • 1
    does it run any faster if your download the entire page first, then run your regex? Are you sure it's the regex taking so long and not the incremental download? – Steven Oct 12 '10 at 00:54
  • Yes Steven, it's the regex stuff. I'm doing some Profiling with Diferente Parsers. – santiagobasulto Oct 12 '10 at 01:59
  • [This answer](http://stackoverflow.com/questions/3918720/how-is-the-best-way-to-extract-the-entire-content-from-a-bufferedreader-object-in/3919073#3919073) also answers this question. – BalusC Oct 12 '10 at 21:17

4 Answers4

13

Hint: Don't use regexes for link extraction or other HTML "parsing" tasks!

Your regex has 6 (SIX) repeating groups in it. Executing it will entail a lot of backtracking. In the worst case, it could even approach O(N^6) where N is the number of input characters. You could ease this a bit by replacing eager matching with lazy matching, but it is almost impossible to avoid pathological cases; e.g. when the input data is sufficiently malformed that the regex does not match.

A far, far better solution is to use some existing strict or permissive HTML parser. Even writing an ad-hoc parser by hand is going to be better than using gnarly regexes.

This page that lists various HTML parsers for Java. I've heard good things about TagSoup and HtmlCleaner.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

I have written simple test for comparing 10 million operation RegExp performance against String.indexof() with the following result:

0.447 seconds
6.174 seconds
13.812080536912752 times regexp longer.

import java.util.regex.Pattern;

public class TestRegExpSpeed {
    public static void main(String[] args) {
        String match = "FeedUserMain_231_Holiday_Feed_MakePresent-1_";
        String unMatch = "FeedUserMain_231_Holiday_Feed_Make2Present-1_";

        long start = System.currentTimeMillis();
        for (int i = 0; i <= 10000000; i++) {
            if (i % 2 == 0) {
                match.indexOf("MakePresent");
            } else {
                unMatch.indexOf("MakePresent");
            }
        }

        double indexOf = (System.currentTimeMillis() - start) / 1000.;
        System.out.println(indexOf + " seconds");

        start = System.currentTimeMillis();
        Pattern compile = Pattern.compile(".*?MakePresent.*?");
        for (int i = 0; i <= 10000000; i++) {
            if (i % 2 == 0) {
                compile.matcher(match).matches();
            } else {
                compile.matcher(unMatch).matches();
            }
        }
        double reaexp = (System.currentTimeMillis() - start) / 1000.;
        System.out.println(reaexp + " seconds");

        System.out.println(reaexp / indexOf + " times regexp longer. ");
    }
}
Peter Lang
  • 54,264
  • 27
  • 148
  • 161
Jonny
  • 47
  • 1
  • 1
  • This is not an answer. Yes, `indexOf()` is faster than regex for that particular task, but why would you use a regex for that? How would the OP use `indexOf()` to solve his problem? – Alan Moore Jul 12 '16 at 23:39
3

Try Jaunt instead. Please don't use regex for this.

Regex use vs. Regex abuse

Regular expressions are not Parsers. Although you can do some amazing things with regular expressions, they are weak at balanced tag matching. Some regex variants have balanced matching, but it is clearly a hack -- and a nasty one. You can often make it kinda-sorta work, as I have in the sanitize routine. But no matter how clever your regex, don't delude yourself: it is in no way, shape or form a substitute for a real live parser.

Source

zengr
  • 38,346
  • 37
  • 130
  • 192
  • 1
    Regex is a very expensive operation. And while you are scraping a website, you will need to parse alot of text. http://www.acorns.com.au/blog/?p=136 – zengr Oct 11 '10 at 23:11
  • The complexity of evaluating regex is something terrible according to the linear complexity of parsing the HTML page – jutky Oct 11 '10 at 23:12
3

All your time, all of it, is being spent here:

 html+=line;

Use a StringBuffer. Better still, if you can, run the match on every line and don't accumulate them at all.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Good hint, anyway, i need to construct all the HTML becouse lines can be broken. – santiagobasulto Oct 12 '10 at 00:05
  • Sorry EJP. It doesn't change anything based on the great time it takes to parse with regex. It does change something, but is nothing comparing it with the overall time. It is a good tip though, i've coded it. – santiagobasulto Oct 12 '10 at 13:49
  • 1
    I know that is an old topic, but what user207421 say is true. I tried your code: it is still taking 7120 ms on my machine. However, when you replace String html with StringBuilder html, it consumes 2200ms. Now, most of the part is to retrieve the data. – Kartal Tabak Apr 26 '19 at 07:24