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

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -