• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex: Pulling out comments from text: Grabbing multiple lines using Pattern, Matcher

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

this is my first post at javaranch and I hope I am posting to the right forum.

Here is the text file:
==================================================================
import java.util.*;

/**
;;
;;Comment 1

*/
code codename1
{
code_contents 1

};

/**
;;
;;Comment 2

*/
code codename2
{
code_contents 2

};
========================================================================

My output should be
==========================================================================
/**
;;
;;Comment 1

*/
/**
;;
;;Comment 2

*/
==========================================================================

I have written my code as shown below, but I am not able to get all the text I need. Need your kind help.
==========================================================
public static void main( String [] args )
{
try
{
String filename = "C:\\workspace\\xxx\\src\\document.txt";

// Read file into String
FileInputStream fis = new FileInputStream(filename);
int len= fis.available();
byte b[]= new byte[len];
fis.read(b);
String content = new String(b);
//System.out.println(content);
StringBuffer comBuf = new StringBuffer();
StringBuffer ruleBuf = new StringBuffer();


// "/**" == (\/\*\*) - Start
// "*/* == (\*\/)- End
// (\/\*\*)(.*)(\*\/) - Capture the comment block
// == (\\/\\*\\*)(.*)(\\*\\/)
//String commRx = "((\\/\\*\\*)(.*)(\\s*)(\\*\\/))";
String commRx = "((\\/\\*\\*)(\\w)(\\s*)(\\*\\/))";
Pattern commPattern = Pattern.compile(commRx,
(Pattern.MULTILINE));
Matcher commMatcher = commPattern.matcher(content);
ArrayList commArr = new ArrayList();

while( commMatcher.find() )
{
System.out.println(commMatcher.group());
}

}
catch( Exception ex )
{
ex.printStackTrace();
}
}

===================================================================


Thanks in Advance!!!
 
Paulo Simon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also folks, I wanted to know if you know of a utility that would help build regular expressions that could be used for java. There are tools out there "Regex Coach" that do this, but I guess it is more for Perl kind of expressions.

For example, the text above, it works for the expression in Regex Coach but for some reason, does not work inside Java

Thanks again and waiting to see new ways to achieve this!!
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The basic pattern for the multiline comments would be "/\\*\\*.*?\\*/", and you would compile it with the DOTALL flag set, not the MULTILINE flag.

As an alternative to Regex Coach, you might try RegexBuddy.
 
Paulo Simon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alan. Works like a charm!!
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic