So I need a regular expression for finding single line and multi line comments, but not in a string. (eg. "my /* string")
for testing (# single line, /* & */ multi line):
# complete line should be found
lorem ipsum # from this to line end
/*
all three lines should be found
*/ but not here anymore
var x = "this # should not be found"
var y = "this /* shouldn't */ match either"
var z = "but" & /* this must match */ "_"
SO does the syntax display really well; I basically want all the gray text.
I don't care if its a single regex or two separates. ;)
EDIT: one more thing. the opposite would also satisfy me, searching for a string which is not in a comment
this is my current string matching: "[\s\S]*?(?<!\\)" (indeed: will not work with "\\")
EDIT2:
OK finally I wrote my own comment parser -.-
And if someone else is interested in the source code, grab it from here: https://github.com/relikd/CommentParser
/* ... */comments; it is not designed to handle#comments (though it does handle C++//comments OK). And it has an inverse mode - print the comments and not the non-comment material. But it is a non-negligible amount of C code that does that.//comment can have an arbitrary number of backslash-newline pairs in between the two slashes. Any regex therefore has to be in a language where you are not reading 'one line at a time' for the C-style comments (the#...EOLcomments are easier).#/*to be a single line comment. And you probably don't want#*/to close an existing comment.