75

I have a file with text and numbers with a length of five (i.e. 12000, 11153, etc.). I want to append all of these numbers with a 0. So 11153 becomes 111530. Is this possible in Notepad++?

I know I can find all numbers with the following regex: [0-9]{5}, but how can I replace these with the same number, plus an appending 0?

In the replacement box I tried the following things:

  • [0-9]{5}0 - Which it took literally, so 11153 was replaced with [0-9]{5}0
  • \10 - I read somewhere that \1 would take the match, but it doesn't seem to work. This will replace 11153 with 0
  • EDIT: \00 - Based on this SO answer I see I need to use \0 instead of \1. It still doesn't work though. This will replace 11153 with

So, I've got the feeling I'm close with the \1 or \0, but not close enough.

CinCout
  • 9,486
  • 12
  • 49
  • 67
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135

4 Answers4

99

You are very near to the answer! What you missed is a capturing group.

Use this regex in "Find what" section:

([0-9]{5})

In "Replace with", use this:

\10

The ( and ) represent a capturing group. This essentially means that you capture your number, and then replace it with the same followed by a zero.

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • 3
    You do not need to capture at all, all OP needs is a `\d{5}` (or `\b\d{5}\b`, or `^\d{5}$`) --> `${0}0`, just use a 0th group defined with the Perl-like backreference. The use of the POSIX style backreference requires the use of a capturing group, true (as POSIX backreferences start from `1` to `9`, thus `\10` is never mistaken for the backreference to the 10th group). – Wiktor Stribiżew Mar 14 '16 at 09:15
  • In my case I don't need the last `0` of `\10`. I just only do capture group with bracket and use `\1` for replacing the capture group text to make result of replacing same as search capture group. – Natta Wang Aug 07 '18 at 06:05
  • On VSCode, `$10` is what worked for me. Related: https://stackoverflow.com/q/43577528/1169233 – Waldir Leoncio Jan 19 '20 at 06:47
9

You are very close. You need to add a capturing group to your regex by surrounding it with brackets. ([0-9]{5})

Then use \10 as the replacement. This is replacing the match with the text from group 1 followed by a zero.

roblovelock
  • 1,971
  • 2
  • 23
  • 41
5

You can use \K to reset.

\b\d{5}\b\K

And replace with 0

See demo at regex101

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Maybe it's a bug in Notepad++, but when I use your regex and use Find Next it does find all the numbers, but when I click the replace with `0`, it doesn't append the 0.. – Kevin Cruijssen Mar 14 '16 at 09:16
  • 1
    @KevinCruijssen: Just checked, zeros are added (v.6.9). – Wiktor Stribiżew Mar 14 '16 at 09:18
  • 1
    @KevinCruijssen I tested it with my np++ v6.8.8 where it worked : ) – bobble bubble Mar 14 '16 at 09:18
  • 1
    When I use Replace All it does indeed work, but when I use Find Next + Replace it doesn't (Notepad++ v6.9). Ah well, I need to replace everything anyway, so +1 as well. I've accept _HappyCoder_'s answer though. As always with regex, there are a lot of different ways to solve the same issue. – Kevin Cruijssen Mar 14 '16 at 09:22
  • 1
    @KevinCruijssen that's fine, up to you which answer prefer : ) in my np++ also the "find next" works (zero length matches as supposed to). – bobble bubble Mar 14 '16 at 09:27
0

General case:

Find what:    (?<=left)(.*?)(?=right)
Replace with: left_append\1right_append
Wrap around [x]

so e.g.

fast=false, out_loc='file',
category=9%s!d,
-->
fast={false}, out_loc={'file'},
category={9%s!d},

i.e. append { and } between = and ,, is accomplished with

(?<==)(.*?)(?=,)
\{\1\}

with escaping as appropriate. Regex further reading.

OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101