> On Jan 3, 2020, at 13:15, anotherhoward <
writer...@gmail.com> wrote:
>
> Here is my input:
> 1
> 2
> 3
> 4
> 5
>
> Here is my search pattern:
> \d\n
>
> Here is the desired output:
> 1,2,3,4,5
So, you need to "separate" the number part from the line break part, so that you can keep the number part in the replacement string but replace the line break part with a ",".
When you check the manual, there is a "Creating subpatterns" section and it says
"Subpatterns provide a means of organizing or grouping complex grep patterns. This is primarily important for two reasons: for limiting the scope of the alternation operator (which otherwise creates an alternation of everything to its left and right), and for changing the matched text when performing replacements."
In all honesty, that's not very clear, plus it can lead to wrong interpretations, because, generally speaking, you create subpatterns so that you can call them in the replacement (and not to change them). The paragraph should probably have "and for *remembering* the matched text when performing replacements." instead.
But the point is, you need that subpattern here:
instead of \d\n, try (\d)\n
now, the \d part is remembered.
And you can call it in the replacement string by "Using Backreferences in Subpatterns", which is the following section in the manual.
Here, you only have one subpattern: (\d)\n
So, to call it in the replacement string, you just use \1, now, for every \d match, \1 will use the number that matched that \d.
Last, you want to really replace "\n" with ",", which is trivial.
So your replacement string should be:
\1,
Search for: (\d)\n
Replace with: \1,
Jean-Christophe Helary
-----------------------------------------------
http://mac4translators.blogspot.com @brandelune