How to substitute substring with dynamic number of spaces

Viewed 102

I would like to use Notepad++ to substitute the decimal digits of a column of numbers with an equivalent amount of white spaces.

For example, this input (vertical bars are there on purpose)

|    1.23|
|  45.678|
|901.2345|

would become

|    1   |
|  45    |
|901     |

Basically, the dot and decimal digits have been substituted with a dynamic number of white spaces: 2+1 spaces for the first row, 3+1 for the second, 4+1 for the third.

I've looked at a few different posts like this and have tried something like: replace \.\d+ with \s+, without success.

3 Answers
  • Ctrl+H
  • Find what: \.(\d)?(\d)?(\d)?(\d)?
  • Replace with: (?1 )(?2 )(?3 )(?4 )
  • CHECK Wrap around
  • CHECK Regular expression
  • Replace all

Explanation:

\.          # a dot
(\d)?       # optional group 1, 1 digit
(\d)?       # optional group 2, 1 digit
(\d)?       # optional group 3, 1 digit
(\d)?       # optional group 4, 1 digit
and so on

Replacement:

            # a space
(?1 )       # if group 1 exists, add a space
(?2 )       # if group 2 exists, add a space
(?3 )       # if group 3 exists, add a space
(?4 )       # if group 4 exists, add a space
and so on

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

You can run a python script within the PythonScript plugin.

The following will work for any number of digits after the dot.

If it is not yet installed, follow this guide

  • Create a script (Plugins >> PythonScript >> New Script)
  • Copy this code and save the file (for example format.py):
import re
def format(match):
    return " " * len(match.group(1))
editor.rereplace('(\.\d+)', format)
  • Open the file you want to modify
  • Run the script Plugins >> PythonScript >> Scripts >> format
  • Done

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Try this in notepad++ if I have understanded you well,

Find: ^([ |\d]*)\.|\G\d{1,1}
Replace with: $1 after $1 there is a single space

Now it is possible with Notepad++
Updated answer

There is a prove with image
enter image description here

Related