Mark Mark - 1 month ago 8
Javascript Question

Notepad++ RegEx : Copy search text and replace same by appending value

I've a big text file which contains page numbers (starting from 997 to 2989) after along with its content.

Ex.

997
....
some-content
....

998
....
some-content
....

999
....
some-content
....

and so on..
.
.
.
2989
....
some-content
....


I want to append value 1 to every page number mentioned above using regex in notepad++ without affecting its actual content so that it'll display like this:

1 997
....
some-content
....

1 998
....
some-content
....

1 999
....
some-content
....

and so on..
.
.
.
1 2989
....
some-content
....


By using following regular expression I'm able to find all the page numbers

/^(\s|\s\s|\s\s\s)(9|1|2)\d+$/gm


But I don't know how can I replace this by appending 1 and keeping same page no as it is.

I've used \1 but its not working and replacing it with blank space.

Any help would be appreciated.

Answer

I suggest using

^(\h{1,3})([912]\d+)$

And replace with 1$1$2.

Details:

  • ^ - start of a line
  • (\h{1,3}) - Group 1 capturing 1 to 3 horizontal whitespaces
  • ([912]\d+) - Group 2 capturing the number starting with 9, 1 or 2 and then having 1 or more digits
  • $ - end of line.

The replacement pattern 1$1$2 contains 1, then $1 that is a backreference to the value kept in Group 1 and $2 referring to the text inside Group 2. With the backreferences, we can re-insert captured subtexts while replacing old text with new one.

enter image description here