Nemin
Nemin

Reputation: 2027

Endless Loop matcher.find()

I am trying to fetch the address from html page. I have a regex and from which I find out the state,city and phone no.

String linearray[] = newdoc.split("\n");
int count = 0;
System.out.println(linearray.length);
while(count<linearray.length)
{
    System.out.println(count);
    Pattern pattern = Pattern.compile("(.*?)(\\d{1,4}(\\s*\\w*)*)(\\s*)(CA|AZ|NY)(\\s*)(\\(?[1-9]\\d{2}\\)?\\s*\\d{3}\\d{4})?(.*?)");
    Matcher matcher = pattern.matcher(linearray[count].trim());
    while (matcher.find()) {
        String state = matcher.group(5);
        String city = matcher.group(2);
        String phone = matcher.group(7);
        System.out.println("state "+state+" city "+city+" phone "+phone+" ");
    }
    count++;
}

When I try to run this code, it goes into an endless loop. Can anyone help me in solving this?

EDIT:

When linearray[count]=="Bombay Garden Newark SanMateo SantaClara &copy; 2011 Bombay Garden All Rights Reserved", my code gets stuck on the line while(matcher.find()). Any idea why it gets stuck there? when I skip that line(by using continue), the code terminates just fine!

Upvotes: 3

Views: 3122

Answers (1)

Dean Taylor
Dean Taylor

Reputation: 41991

Your regular expression leads to "catastrophic backtracking", making it too complex to be run to completion.

Consider rewriting your regex to be more possessive.

Upvotes: 3

Related Questions