Cannot replace source text using GREP

29 views
Skip to first unread message

anotherhoward

unread,
Jan 2, 2020, 11:17:02 PM1/2/20
to BBEdit Talk
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

What GREP expression will convert my input to my desired output?

Thanks,
Howard

anotherhoward

unread,
Jan 2, 2020, 11:17:02 PM1/2/20
to BBEdit Talk
Here is my input:
1
2
3
4
5

Here is my search pattern:
\d\n

It finds all the source text.

Here is how I want my output to appear:
1,2,3,4,5

What replace pattern do I need to convert my input to my output?

Thanks,
Howard

Jean-Christophe Helary

unread,
Jan 2, 2020, 11:55:48 PM1/2/20
to bbe...@googlegroups.com


> 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


Kerri Hicks

unread,
Jan 3, 2020, 7:44:52 AM1/3/20
to bbe...@googlegroups.com
Yes, everything Jean-Christophe said.

For this use case, though, you don't really need grep (as fun as regular expressions are!), because what you're really doing is replacing a line break with a comma. The simplest way I've found to do that is to select a line break from the editor (make a blank line, select the blank line, copy the blank line) and paste it into the Find: box of the Find pane. Then put a comma into the Replace: box.

Tick the "Selected text only" box in the search pane (again, assuming there's other text in the file that you don't want to convert), select the list of numbers you want to convert, and if you "Replace All" you should end up with your desired result.

--Kerri

--
This is the BBEdit Talk public discussion group. If you have a
feature request or need technical support, please email
"sup...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/C881BBFF-D870-4F2F-BB24-036C29991682%40traduction-libre.org.

anotherhoward

unread,
Jan 3, 2020, 9:39:53 AM1/3/20
to BBEdit Talk
Thank you both Jean-Christophe and Kerri. Both your solutions worked.


On Friday, January 3, 2020 at 7:44:52 AM UTC-5, Kerri Hicks wrote:
Yes, everything Jean-Christophe said.

For this use case, though, you don't really need grep (as fun as regular expressions are!), because what you're really doing is replacing a line break with a comma. The simplest way I've found to do that is to select a line break from the editor (make a blank line, select the blank line, copy the blank line) and paste it into the Find: box of the Find pane. Then put a comma into the Replace: box.

Tick the "Selected text only" box in the search pane (again, assuming there's other text in the file that you don't want to convert), select the list of numbers you want to convert, and if you "Replace All" you should end up with your desired result.

--Kerri

To unsubscribe from this group and stop receiving emails from it, send an email to bbe...@googlegroups.com.

Marek Stepanek

unread,
Jan 3, 2020, 9:40:06 AM1/3/20
to bbe...@googlegroups.com

Happy New Year 2020!

Search with grep: 

^\d\n

Replace with:

\01,

Hope this helps!




--
This is the BBEdit Talk public discussion group. If you have a
feature request or need technical support, please email
"sup...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/e88823d3-79c5-4daf-aea6-4ef566285951%40googlegroups.com.

Massimo Rainato

unread,
Jan 3, 2020, 10:42:24 AM1/3/20
to bbe...@googlegroups.com
(\d)(\n) with \1, 

Imho

--

Howard Rosenberg

unread,
Jan 3, 2020, 10:42:32 AM1/3/20
to bbe...@googlegroups.com
Hi Marek.

When I ran your expression in BBEdit I got this result:

,,,,5

Howard
On Jan 3, 2020, 9:40 AM -0500, bbe...@googlegroups.com, wrote:

^\d\n

Marek Stepanek

unread,
Jan 3, 2020, 1:50:22 PM1/3/20
to bbe...@googlegroups.com
Sorry Howard!

I was in a hurry on the way to my work. You have of course capture the numbers like follows:

^(\d)\n

Replace with 

\01,

Please read the BBEdit manual of GREP. This are really fundamental questions you are asking. 

Have a nice evening


marek



--
This is the BBEdit Talk public discussion group. If you have a
feature request or need technical support, please email
"sup...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Howard Rosenberg

unread,
Jan 3, 2020, 3:04:27 PM1/3/20
to bbe...@googlegroups.com
Thanks Massimo.
On Jan 3, 2020, 10:42 AM -0500, bbe...@googlegroups.com, wrote:

(\d)(\n)
Reply all
Reply to author
Forward
0 new messages