Home > Software design >  regexp expressions to format *.properties file properly
regexp expressions to format *.properties file properly

Time:05-11

I need to clean up/reformat *.properties files. I would like to remove the extra spaces AND tab characters around the equal signs (=) and from the end of lines but I would like to keep the original structure of the files (do not remove newline characters). If I use my IDE to reformat that files it removes the new line characters as well which is not acceptable for me. So I use the find and replace function of my IDE (IntelliJ) with regexp to deal with this task.

This regexp removes properly the whitespaces from the end of the line:

[ \t] $

But I am not able to write a proper regexp that removes the extra whitespaces and tabs around the equal symbol properly.

Example:

original:
   log4j.appender.MAIN_LOG          =      org.apache.log4j.rolling.RollingFileAppender

expected:
   log4j.appender.MAIN_LOG=org.apache.log4j.rolling.RollingFileAppender

Could you please suggest to me regexp for this scenario?

CodePudding user response:

I've tested this regex on my Intellij IDEA. This will match every space and tab before your equal sign (excluded), after your equal sign (excluded) and at the end of each line by using a positive lookahead and a positive lookbehind to exclude the equals.

([ \t] (?==)|(?<==)[ \t] |[ \t] $)

Here, there's also a link to test it

https://regex101.com/r/iO4sVB/1

  • Related