'Notepad++ regexp search and replace
I have a series of 12 comma-separated digits and want to extract the first, 4th, 7th, and 10th digits. How to do this without writing it out too much ?
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
to
1, 4, 7, 10,
And
148, 79, 562, 184, 28, 487, 186, 16, 792, 200, 12, 956,
to
148, 184, 186, 200,
And yes there is a comma and space at the end of each line, so that the regexp \d+, repeats exactly 12 times per line
I want to avoid writing (\d+, )(\d+, ){2} 4 times and asking it to replace with $1 $3 $5 $7
Solution 1:[1]
find what: [0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, ([0-9]{1,3}), [0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, ([0-9]{1,3}), [0-9]{1,3}, [0-9]{1,3}, [0-9]{1,3}, ([0-9]{1,3}),
Replace with: \1, \2, \3,
Solution 2:[2]
You can not dynamically get the capture group numbers by repeating a capture group. You would have make the capture groups part of the pattern.
^(\d+, )(?:\d+, ){2}(\d+, )(?:\d+, ){2}(\d+, )(?:\d+, ){2}(\d+, )\d+, \d+, $
Then replace with
$1$2$3$4
If you want to match the values, you could make use of lookarounds asserting a number of repetetive parts to the right until the end of the string.
Using \h* matches optional horizontal whitespace characters.
\d+,(?=(?:(?:\h*\d+,){3})*(?:\h*\d+,\h){2}$)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivan |
| Solution 2 | The fourth bird |
