Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用正则表达式在文件中查找短语_Java_Regex - Fatal编程技术网

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

Java 使用正则表达式在文件中查找短语,java,regex,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: 节目 您或者需要将整个文件放入一个字符串中(将

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

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:
节目


您或者需要将整个文件放入一个字符串中(将该字符串添加到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

至少在一次阅读中,我不太清楚你想做什么,想要什么。请尝试重新表述您的问题。
Pattern.compile(“class[\\s]+Main”,Pattern.MULTILINE | Pattern.DOTANY)我尝试在文件中查找短语。

          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();
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