regex - Java regular expression find all A except B -
here's case: want use java remove matching "//[^\n]*" except matches "\"[^\n]*//[^\n\"]\"". pretty much, need remove comment style input, except if contained within string. tried regex "(//[^\n]*)-(\"[^\n]*//[^\n]*\")", not replace anything.
this perl group many years ago, modified bit preserve formatting.
there simpler version of not preserve formatting.
this 1 uses multi-line mode because of preservation.
also, if don't have single quoted strings, take out part.
it match comments or non-comments.
run it;
- set multi-line mode
- do global replace
$2
and that's it.
# raw: ((?:(?:^[ \t]*)?(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/(?:[ \t]*\r?\n(?=[ \t]*(?:\r?\n|/\*|//)))?|//(?:[^\\]|\\(?:\r?\n)?)*?(?:\r?\n(?=[ \t]*(?:\r?\n|/\*|//))|(?=\r?\n))))+)|("(?:\\[\s\s]|[^"\\])*"|'(?:\\[\s\s]|[^'\\])*'|(?:\r?\n|[\s\s])[^/"'\\\s]*) # quoted: "((?:(?:^[ \\t]*)?(?:/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/(?:[ \\t]*\\r?\\n(?=[ \\t]*(?:\\r?\\n|/\\*|//)))?|//(?:[^\\\\]|\\\\(?:\\r?\\n)?)*?(?:\\r?\\n(?=[ \\t]*(?:\\r?\\n|/\\*|//))|(?=\\r?\\n))))+)|(\"(?:\\\\[\\s\\s]|[^\"\\\\])*\"|'(?:\\\\[\\s\\s]|[^'\\\\])*'|(?:\\r?\\n|[\\s\\s])[^/\"'\\\\\\s]*)" ( # (1 start), comments (?: (?: ^ [ \t]* )? # <- preserve formatting (?: /\* # start /* .. */ comment [^*]* \*+ (?: [^/*] [^*]* \*+ )* / # end /* .. */ comment (?: # <- preserve formatting [ \t]* \r? \n (?= [ \t]* (?: \r? \n | /\* | // ) ) )? | // # start // comment (?: # possible line-continuation [^\\] | \\ (?: \r? \n )? )*? (?: # end // comment \r? \n (?= # <- preserve formatting [ \t]* (?: \r? \n | /\* | // ) ) | (?= \r? \n ) ) ) )+ # grab multiple comment blocks if need ) # (1 end) | ## or ( # (2 start), non - comments " (?: \\ [\s\s] | [^"\\] )* # double quoted text " | ' (?: \\ [\s\s] | [^'\\] )* # single quoted text ' | (?: \r? \n | [\s\s] ) # linebreak or other char [^/"'\\\s]* # chars doesn't start comment, string, escape, # or line continuation (escape + newline) ) # (2 end)
Comments
Post a Comment