regexcharacter-replacement

How to reduce code's margin (=number of spaces) using Regex?


Recently, on Notepad++, I want to reduce number of spaces at begin of lines.

I searched a Regular Expression, but I don't find any.

On a array(=tab) of Notepad++, I have following lines

<html>
    <body>
        <p>ligne normale</p>
        <p>ligne      with    2 spaces  </p>

and I want to reduce (= divide by 2 number of spaces) only spaces before '<' character on all lines.

Waiting result is following

<html>
  <body>
    <p>ligne normale</p>
    <p>ligne      with    2 spaces  </p>

I tried following Regex and string replacement

Search: \s{2}(?<=^\s*)
Replace: (= one space)

but Notepad++ refuses this Regex !

I have then tested the same Regex on a C++ program that is using PCRE version 7.8. It indicates that look-behind can only contain fix length string !

Is there a Regex that can do the job ?

PS: the following Regex that use a look-ahead to the job ... partially !

\s{2}(?=(\s{2})*<)

The last 2 spaces in last line, just before last

are not in the margine and are removed while they must be kept !


Solution

  • I have not tried it on Notepad++, but this only uses very basic regular expression features in order to halve the number of start-of-line spaces:

    Find what:      ^(\s+)\1
    Replace with:   $1