簡體   English   中英

無盡的循環matcher.find()

[英]Endless Loop matcher.find()

我想從html頁面獲取地址。 我有一個正則表達式,我從中發現州,城市和電話號碼。

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++;
}

當我嘗試運行此代碼時,它會進入無限循環。 任何人都可以幫我解決這個問題嗎?

編輯:

linearray[count]=="Bombay Garden Newark SanMateo SantaClara &copy; 2011 Bombay Garden All Rights Reserved" ,我的代碼卡在了線上while(matcher.find()) 知道為什么會卡在那里嗎? 當我跳過那一行(通過使用繼續),代碼終止就好了!

你的正則表達式會導致“ 災難性的回溯 ”,使其過於復雜而無法完成。

考慮重寫你的正則表達式以更具有占有欲

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM