java - RegEx - match all whitespaces (including line breaks) which are NOT the end of a preprocessor line -


i remove unnecessary whitespace in file, including line breaks, if line break not end of preprocessor line. preprocessor commands same in c though language of file sqf, doesn't matter in context. i'm doing stuff in java, reasons of readability, i'm putting regex here in plain , not java strings.

this example of file might like:

#include "somefile.ext" #define idd             idd_interact #define elements_count  2  #define frame_x         ((1 - frame_w) / 2) #define frame_y         ((1 - frame_h) / 2)  _anumber = 0;  if (_somevar == someglobalvar) {  }; 

i've tried one: \\s+(?!#) result this:

#include "somefile.ext"  #define idd idd_interact  #define elements_count 2  #define frame_x ((1 - frame_w) / 2)  #define frame_y ((1 - frame_h) / 2) _anumber = 0; if (_somevar == someglobalvar) { };  

after last preprocessor line, line break gets remove too. want remain, want result looking this:

#include "somefile.ext"  #define idd idd_interact  #define elements_count 2  #define frame_x ((1 - frame_w) / 2)  #define frame_y ((1 - frame_h) / 2)  _anumber = 0; if (_somevar == someglobalvar) { };  

i tried negative lookbehind, come invalid regex (quatifiers , lookarounds don't seem each other):

(?<!(#[^\s]+\s+[^\s]+[^\n]*))[\s] 

now, i'm still rather new regex, hence i'm pretty @ end of knowledge here. there solution that? i'd grateful!

how about:

^((?!#).*?) *\n\s*| +$| +( ) 

which give this, when replaced globally, in multiline mode, \1\2:

#include "somefile.ext" #define idd idd_interact #define elements_count 2 #define frame_x ((1 - frame_w) / 2) #define frame_y ((1 - frame_h) / 2) _anumber = 0;if (_somevar == someglobalvar) {}; 

beware replace whitespace in string literals. regex poor choice working on inputs complex nested grammar (like java source code). recommend existing, tested code minification tools have concept of doing. using regex on source code going dark room sharp tools lying around. on roller skates.


Comments