1

Is it possible using regular expressions to to replace multiple lines w/ the same number of blank lines?

In context, I need to be able to change the following:

01    /*------------------------------------
02    Some history stuff goes here
03    Some history stuff goes here
04    Some history stuff goes here
05    Some history stuff goes here
06    Some history stuff goes here
07    --------------------------------------*/
08    int main void()
09    {
10       printf("Hello, World!");
11
12       return 0;
13    }

Into the following:

01    
02    
03    
04    
05    
06    
07    
08    int main void()
09    {
10       printf("Hello, World!");
11
12       return 0;
13    }

Right now I only have the following regex pattern, but it only replaces lines 01-07 with a single line:

\/\*.*?\*\/
(. matches newline checked)

If it helps, Im using Notepad++'s Find in Files feature since I need to do it in multiple files in a folder.

slimjourney
  • 99
  • 10
  • @sweaver2112 "ff" is shortcut for "following" – slimjourney Sep 28 '18 at 04:45
  • Did you check [this](https://stackoverflow.com/questions/3866034/removing-empty-lines-in-notepad#20953496) ? – STIKO Sep 28 '18 at 04:50
  • @STIKO thanks but Im not trying to remove "empty lines" im trying to replace "block comments" with the same number of "blank lines" – slimjourney Sep 28 '18 at 04:53
  • i think you'd need a few script commands to get this done, regex doesn't offer a "number of lines matched" variable in the substitution. – Scott Weaver Sep 28 '18 at 04:53
  • @sweaver2112 any suggestions how to get started? – slimjourney Sep 28 '18 at 04:58
  • use the first pattern, then against that commented text, match again against `/.*\n/` to see how many lines you have, insert that many newlines, or something like that. (virtually any language/shell would work, don't' know what your env is) – Scott Weaver Sep 28 '18 at 05:05
  • @slimjourney I don't think you can get the number of lines, but you could replace the whole comment section using this `^/\*[-]*[^\*/]*\*/` – STIKO Sep 28 '18 at 05:28

2 Answers2

1

Since you're using notpad++ I'm assuming you're on windows. You may need to use power shell to achieve this. First read the file line by line, found here

$reader = [System.IO.File]::OpenText("my.log")
while($null -ne ($line = $reader.ReadLine())) {
    $line
}

Then do some logic when the line starts with /* and ends with */, source here

$Group.StartsWith("/*")
STIKO
  • 1,995
  • 1
  • 10
  • 9
1

This perl one-liner does the job:

perl -ne 'if (/\/\*/ .. /\*\//) {print "\n"} else {print}' file > output

under windows, change single quotes with double quotes

perl -ne "if (/\/\*/ .. /\*\//) {print \"\n\"} else {print}" file > output
Toto
  • 89,455
  • 62
  • 89
  • 125