permaculture playing cards*
The moose likes Java in General and the fly likes Pattern to match multiline strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pattern to match multiline strings " Watch "Pattern to match multiline strings " New topic
Author

Pattern to match multiline strings

Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

Hi all,
I am writing a program to delete all the comments from a given java file.

I was able to remove the single line comments using pattern - Pattern p= Pattern.compile("//(.)*");

But matching multiline comments is getting trickier.
I am using - Pattern p= Pattern.compile("/\\*.*\\*/", Pattern.DOTALL); - Basically match anything between /* and */ and include new lines.

But If I have something like below, it is matching this entire thing as a single match and I am losing the code between the comments.
/**
*comment1
*/
codeline1
line2
/**
*comment2
*/

Could some one help me how to rewrite this pattern so that I dont delete the code between the comments?

Thanks in advance.


If it's easy.. then everybody would do it!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

Srikanth Madasu wrote:
I am writing a program to delete all the comments from a given java file.

I was able to remove the single line comments using pattern - Pattern p= Pattern.compile("//(.)*");

But matching multiline comments is getting trickier.
I am using - Pattern p= Pattern.compile("/\\*.*\\*/", Pattern.DOTALL); - Basically match anything between /* and */ and include new lines.

But If I have something like below, it is matching this entire thing as a single match and I am losing the code between the comments.
/**
*comment1
*/
codeline1
line2
/**
*comment2
*/

Could some one help me how to rewrite this pattern so that I dont delete the code between the comments?



The easiest option is to use a reluctant qualifier.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

Thanks Henry.

It worked. Just changed pattern to include reluctant quantifier "?" .

Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Pattern to match multiline strings
 
Similar Threads
Regex and startsWith
How to search a pattern in a large file using java
Regex: Extracting content from multiline texts.
Pattern matches but never replaces
How to enable multiline option for regex searching in Eclipse?