Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading Comments inside strings  RSS feed

 
nick angel
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Afternoon.

I have been working on reading through a file and retrieveing information. In this file are also included comments( // and /*...*/). I have so far been able to remove comments. However I later realised that one can have the following:

String s1= new String("This is what a comment is \* blah blah *\");

I used the following:

Pattern remComment = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL | Pattern.MULTILINE);
for normal comments. But when they are placed in a string they should be kept.

I was thinking of creating a method that would accept a line I have read from a file and determine if I shoudl keep it or not.

In this method I could use string.indexOf() method to determine if the line contains " and /*. However this process would lead to problems shoudl we have the following:

String s1=new String ("blah blah"); /*...*/

I am getting a little confused. Could someone help guide me.

Thank you
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you're going to have more trouble if the /* and */ are on different lines, aren't you? If you're trying to process actual Java code then breaking it up into lines is an unnatural thing to do. If I were doing that, I would use a StreamTokenizer. It's got all sorts of hard-coded features for parsing Java-like languages.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!