使用正则表达式在文件中查找短语

时间:2010-10-27 05:19:40

标签: java regex

使用正则表达式搜索文件中的短语

So...

Task:

I have some phrase. the phrase contains 2 words. Phrase is devided into two words with next symbols:

[\s]*

How can i find the phrase using regular expression?

This code doesn't work on a file:
// file: Main.java
    class 


    Main {
    }

程序


          Pattern pattern = Pattern.compile("class[\\s]+Main");

          BufferedReader input = new BufferedReader ( new FileReader( "Main.java" ) );

          int id = 0;

          for ( String line = input.readLine(); line != null; line = input.readLine() )
          {
             ++id;

             Matcher matcher = pattern.matcher( line );


             if ( matcher.find() )
             {
                System.out.println("number = " + id );

                System.out.println("start = " + matcher.start() );
                System.out.println("end   = " + matcher.end() );
                System.out.println( );
             }
          }

          input.close();

1 个答案:

答案 0 :(得分:0)

您需要将整个文件放入一个字符串中(在for循环中将字符串添加到StringBuilder,然后在结尾处对结果进行一次匹配),或者您需要添加一个案例将最后一个单词存储在一行中,将第一个单词存储在包含任何单词的下一行,并检查这些单词。最简单但可能不是最快的方法是:

String lastline = ""
// For loop begins
  String[] ll_parts = lastline.split("\\s")
  lastline = line;
  line = ll_parts[ll_parts.length-1] + line;
// Now do the match