-2

Webページで機能する正規表現マッチングのコードは次のとおりです。

public class RegexTestHarness {

    public static void main(String[] args) {

        File aFile = new File("/home/darshan/Desktop/test.txt");
        FileInputStream inFile = null;
        try {
            inFile = new FileInputStream(aFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }

        BufferedInputStream in = new BufferedInputStream(inFile);
        DataInputStream data = new DataInputStream(in);
        String string = new String();
        try {
            while (data.read() != -1) {
                string += data.readLine();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Pattern pattern = Pattern
                .compile("<div class=\"rest_title\">.*?<h1>(.*?)</h1>");
        Matcher matcher = pattern.matcher(string);
        boolean found = false;
        while (matcher.find()) {
            System.out.println("Name: " + matcher.group(1) );
            found = true;
        }
        if(!found){
            System.out.println("Pattern Not found");
        }
    }
}

しかし、正規表現をテストしているクローラー コードでは同じコードが機能しません。私のクローラー コードは次のとおりです:(私は Websphinx を使用しています)

// Our own Crawler class extends the WebSphinx Crawler
public class MyCrawler extends Crawler {

    MyCrawler() {
        super(); // Do what the parent crawler would do
    }

    // We could choose not to visit a link based on certain circumstances
    // For now we always visit the link
    public boolean shouldVisit(Link l) {
        // String host = l.getHost();
        return false; // always visit a link
    }

    // What to do when we visit the page
    public void visit(Page page) {
        System.out.println("Visiting: " + page.getTitle());
        String content = page.getContent();

        System.out.println(content);

        Pattern pattern = Pattern.compile("<div class=\"rest_title\">.*?<h1>(.*?)</h1>");
        Matcher matcher = pattern.matcher(content);
        boolean found = false;
        while (matcher.find()) {
            System.out.println("Name: " + matcher.group(1) );
            found = true;
        }
        if(!found){
            System.out.println("Pattern Not found");
        }
    }
}

これは、クローラーを実行するための私のコードです:

public class WebSphinxTest {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {

        System.out.println("Testing Websphinx. . .");

        // Make an instance of own our crawler
        Crawler crawler = new MyCrawler();
        // Create a "Link" object and set it as the crawler's root
        Link link = new Link("http://justeat.in/restaurant/spices/5633/indian-tandoor-chinese-and-seafood/sarjapur-road/bangalore");
        crawler.setRoot(link);

        // Start running the crawler!
        System.out.println("Starting crawler. . .");
        crawler.run(); // Blocking function, could implement a thread, etc.

    }

}

クローラーコードについて少し詳しく説明します。shouldvisit(Link link)リンクにアクセスするかどうかをフィルタリングします。visit(Page page)ページを取得したときに何をするかを決定します。

上記の例では、test.txt と content に同じ文字列が含まれています。

4

1 に答える 1

3

RegexTestHarnessファイルから行を読み込んで、改行なしで行を連結してから、一致を行います(改行readLine()なしで行の内容を返します!)。

したがって、MyCrawlerクラスの入力には、入力に改行文字が含まれている可能性があります。また、正規表現のメタ文字.はデフォルトでは改行文字と一致しないため、 では機能しませんMyCrawler

これを修正するには、メタ文字(?s)を含むすべてのパターンの from を追加します。.そう:

Pattern.compile("<div class=\"rest_title\">.*?<h1>(.*?)</h1>")

次のようになります。

Pattern.compile("(?s)<div class=\"rest_title\">.*?<h1>(.*?)</h1>")

DOT-ALL フラグ は(?s).改行文字を含む任意の文字と一致します。

于 2011-09-07T19:07:49.087 に答える