Home > Software engineering >  Regex - Remove space chars whenever there are more than one in succession, but exclude all lines com
Regex - Remove space chars whenever there are more than one in succession, but exclude all lines com

Time:02-26

Let's say I have a few lines as follows:

01090   C   -------CALCULATION OF SOMETHING--
01100   "SOME.VARIABLE"   =  "SOME.OTHER.VARIABLE"   2
01110   IF("SOME.VARIABLE" .NE.  "SOME.VALUE")  THEN   ON("SOME.MACHINE")

I would like to go through the program and remove all of the space characters that have more than one in succession. For example, line 01100 has three (3) space characters before the "=" and two (2) after. In line 01110, there are several different locations with more than 1 consecutive space char. I would like to replace them with just a single space char. I do NOT want to remove/alter the spaces that are contained within the commented line 01090.

All lines begin with 5 digits, all lines have a tab following the line number, and only commented lines have a "C" or a "c" that denotes them as commented out.

I am using Sublime3, and boost regex. I have tried things like:

(?!\t[Cc] )[ ]{2,}
(?!\t[Cc])[ ]{2,}

I can't seem to determine how to negate an entire line without also capturing an entire line.

I tried putting a caret in the beginning as well, but that didn't seem to help.

Basically, if the line has a "TAB" followed by a "c" or a "C", then ignore the entire thing. Otherwise, any two or more consecutive space chars are located and replaced with a single space char.

CodePudding user response:

You have to add a negative lookahead and a negative lookbehid to your regex. Try something like this.

(?<![Cc])\s{3,}(?![Cc])

CodePudding user response:

You might use

^\d{5}\t[cC] .*$(*SKIP)(*FAIL)|\h{2,}
  • ^ Start of string
  • \d{5}\t Match 5 digits and a tab
  • [cC] Match either c or C and a space
  • .*$ Match the rest of the line
  • (*SKIP)(*FAIL) Skip the match
  • | Or
  • \h{2,} Match 2 or more horizontal whitespace chars

In the replacement use a single space.

Regex demo

  • Related