I'm trying to replace all kinds of comments (single, inline & multiline). The initial regex worked absolutely fine when //
& /* */
didn't occur between any kind of quotes, ""
or """"""
. When I modified the regex a bit to handle and exclude the occurances of // between quotes, its failing and messing up the initial code structure as well.
Here was my initial regex (Regex:1): (?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)
Here was the regex I tweaked to try and handle the single line comments inside quotes (Regex:2): (?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|[^\"](?://.*)[^\"]
Consider this sample data:
// Comment 1
/* Multiline comments
ends here */ Some text
Random statement // something else
import something..
import something else /* few random stuff
that goes on */ /* Lets try this again */
Text to show
val tryThis = " something // else "
val tryAgain = "12345"
val again = " /* kskokds // */ "
Actual result of Regex:1 =>
Some text
Random statement
import something..
import something else
Text to show
val tryThis = " something
val tryAgain = "12345"
val again = " "
Actual result of Regex:2 =>
// Comment 1
Some text
Random statementimport something..
import something else
Text to show
val tryThis = " somethingval tryAgain = "12345"
val again = " "
Expected Result =>
Some text
Random statement
import something..
import something else
Text to show
val tryThis = " something // else "
val tryAgain = "12345"
val again = " /* kskokds // */ "