Jump to content

using regular expression for searching phrase in the file

- - - - -

  • Please log in to reply
2 replies to this topic

#1
vachovsky

vachovsky

    Newbie

  • Members
  • Pip
  • 8 posts
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();

  



#2
discomonk

discomonk

    Newbie

  • Members
  • Pip
  • 7 posts
The reason your code didn't work is because you are trying to match a multi-line pattern against each line separately. To fix it, you would have to buffer all of the input lines using StringBuilder and then match against them together, like the modified version of your code:


public class Regexp {


	public static void main(String[] args) throws IOException {

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


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


		StringBuilder bldr = new StringBuilder(1024);

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

		{

			bldr.append(line);

		}

		

		Matcher matcher = pattern.matcher( bldr.toString() );



		if ( matcher.find() )

		{

			

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

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

			System.out.println( );

		}



		input.close();


	}

}




#3
vachovsky

vachovsky

    Newbie

  • Members
  • Pip
  • 8 posts
[english]
Yes, this solution is obvious. But, the file is too big, >= 400 mb ....

I think, that i will read file using something like
char c = scanner.nextChar()
and
 if ( c == "class".charAt(0) ) { ... } 

[russian]
Да, это решение очевидно. Но файл слишком большой, чтоб всё его содержимое ложить в строчку. Минимальный размер файла в 400 mb...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users