Kevin Cruijssen
Kevin Cruijssen

Reputation: 9336

Notepad++ Replace regex match for same text plus appending character

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:

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

Upvotes: 76

Views: 95740

Answers (4)

OverLordGoldDragon
OverLordGoldDragon

Reputation: 19836

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.

Upvotes: 0

roblovelock
roblovelock

Reputation: 1981

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.

Upvotes: 9

CinCout
CinCout

Reputation: 9619

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.

Upvotes: 101

bobble bubble
bobble bubble

Reputation: 18565

You can use \K to reset.

\b\d{5}\b\K

And replace with 0

See demo at regex101

Upvotes: 4

Related Questions