Anyone have an example for removal of Java like comments

Does anyone have a solution to remove comments from text?
I have not has success with regular expressions nor with the parser combinator.


You could try Scalaparse as a starting point:

Here’s the relevant part of the grammar handling comments:

Besides, what is this for? A one time job? You could also use cpp / gcc to remove Java style comments from your source files. (e.g. cpp -P -fpreprocessed your-file.scala > output.scala)

BTW, tackling this with a regex is futile, it is not context sensitive (think of comment characters inside of string literals).

And maybe you should say what you mean with “text”?!

@cbley I tried Fastparse but because I am using Scala 3 (no Scala 2 macros), it was not possible.

I am aware of the literals (found a reference to thees in a Gitter). I tried something similar here:

see [SOLVED] Parser combinator: removing comments

Not a one-time job. Preprocessing text before parsing in Scala.

I need to strip illegal comments from a JSON file.

I think my problem is knowing how to combine the literals. My example above fails with:

    val text1 =

Which JSON parser are you using? Is it based on jackson? This would be as easy as parser.enable(ALLOW_COMMENTS)

ujson. I don’t think it has such a flag. 8-(

Then maybe

Thanks taking a look now.