Question

Have some way to numerate the following text

('xx', 'link 001'),

('xx', 'link 002'),

('xx', 'link 100');

to

('001', 'link 001'),

('002', 'link 002'),

('100', 'link 100'),

like a while

While($x = 001; $x <= 100) {
$xx == $x++;
}
Was it helpful?

Solution

Assuming your text is laid out exactly like you posted:

('xx', 'link 001'),

('xx', 'link 002'),

('xx', 'link 100');

The following Regex search should work:

Find:

'\w+', 'link (\d+)'

Replace:

'\1', 'link \1'

Breaking this down bit-by-bit for you,

' - searches for '
\w - searches for an alphanumeric character or an underscore.
+ - instructs Notepad++ to find the preceding sequence one or more times. In combination with the \w, this looks for one or more alphanumeric characters/underscores. This will match your xx.
' - another '. So at this point, we're looking for ', followed by one or more alphanumeric characters, followed by '.
, 'link - another literal search, this time for a comma followed by a space followed by a ' followed by the word link followed by a space.
( - starts a grouping. Anything between parentheses (()) can be later identified using \1, \2, etc. This one will be identified by \1 since it's our first group.
\d - searches for a digit.
+ - again, matches one or more of the preceding characters. So we're looking for one or more digits.
) - closes the grouping. \1 will now hold the results of a search for one or more digits.
' - the final ' in your text.

Now for the replace. This is much simpler. In a nutshell, it just takes the digits the "find" regex found and replaces the multi-character sequence with them, then puts everything else back the way it was.

' - another literal, the first '.
\1 - holds the results of the multi-digit search the "find" regex used. So for link 001, this will hold 001; for link 002, this will hold 002; etc. Here's where the magic happens: the alphanumeric sequence found by \w+ will be replaced by the digits \d+ found.
', 'link - another literal. This will insert ', 'link. Basically we're just putting back the text that was already there.
\1 - the same multi-digit grouping the last \2 was. Once again, 001 for link 001, 002 for link 002, etc. We're just replacing the number that was already there... with itself.
' - the closing '.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top