They do not preserve formatting
Preferred path (creates NULL if group 1 does not map)
works on the golang playground -
# https://play.golang.org/p/yKtPk5QCQV # fmt.Println(reg.ReplaceAllString(txt, "$1")) # (?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//[^\n]*(?:\n|$))|("[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]*)*'|[\S\s][^/"'\\]*) (?: # Comments /\* # Start /* .. */ comment [^*]* \*+ (?: [^/*] [^*]* \*+ )* / # End /* .. */ comment | // [^\n]* # Start // comment (?: \n | $ ) # End // comment ) | ( # (1 start), Non - comments " [^"\\]* # Double quoted text (?: \\ [\S\s] [^"\\]* )* " | ' [^'\\]* # Single quoted text (?: \\ [\S\s] [^'\\]* )* ' | [\S\s] # Any other char [^/"'\\]* # Chars which doesn't start a comment, string, escape, or line continuation (escape + newline) ) # (1 end)
Alternative method (group 1 always matches, but may be empty)
works on the golang playground -
# https://play.golang.org/p/7FDGZSmMtP # fmt.Println(reg.ReplaceAllString(txt, "$1")) # (?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//[^\n]*(?:\n|$))?((?:"[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]*)*'|[\S\s][^/"'\\]*)?) (?: # Comments /\* # Start /* .. */ comment [^*]* \*+ (?: [^/*] [^*]* \*+ )* / # End /* .. */ comment | // [^\n]* # Start // comment (?: \n | $ ) # End // comment )? ( # (1 start), Non - comments (?: " [^"\\]* # Double quoted text (?: \\ [\S\s] [^"\\]* )* " | ' [^'\\]* # Single quoted text (?: \\ [\S\s] [^'\\]* )* ' | [\S\s] # Any other char [^/"'\\]* # Chars which doesn't start a comment, string, escape, or line continuation (escape + newline) )? ) # (1 end)
Formatting Cadilac - Preserves
(Unfortunately, this cannot be done in the Golang, because the Golan cannot make statements)
Added that you switch to another regular expression engine.
# 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]*) # delimited: /((?:(?:^[ \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]* )? # <- To preserve formatting (?: /\* # Start /* .. */ comment [^*]* \*+ (?: [^/*] [^*]* \*+ )* / # End /* .. */ comment (?: # <- To preserve formatting [ \t]* \r? \n (?= [ \t]* (?: \r? \n | /\* | // ) ) )? | // # Start // comment (?: # Possible line-continuation [^\\] | \\ (?: \r? \n )? )*? (?: # End // comment \r? \n (?= # <- To preserve formatting [ \t]* (?: \r? \n | /\* | // ) ) | (?= \r? \n ) ) ) )+ # Grab multiple comment blocks if need be ) # (1 end) | ## OR ( # (2 start), Non - comments " [^"\\]* # Double quoted text (?: \\ [\S\s] [^"\\]* )* " | ' [^'\\]* # Single quoted text (?: \\ [\S\s] [^'\\]* )* ' | (?: \r? \n | [\S\s] ) # Linebreak or Any other char [^/"'\\\s]* # Chars which doesn't start a comment, string, escape, # or line continuation (escape + newline) ) # (2 end)
source share