Handling dollar signs in search and replace

1,816 views
Skip to first unread message

Duncan Thorne

unread,
May 25, 2021, 9:15:56 PM5/25/21
to BBEdit Talk
I'm stumped when it comes to reformatting a text line that begins with a $ symbol. I want to replace the preceding line's line break, followed by the new line's $, with a tab-$.
For instance:
... 2021
$0.23

changed to:
... 2021 (tab) $0.23

BBEdit search-and-replace treats $ as a special search symbol, I gather to do with matching. Is there a way to make it see $ as a dollar sign?

Kerri Hicks

unread,
May 25, 2021, 9:19:57 PM5/25/21
to bbe...@googlegroups.com
When using grep, $ anchors the match at the end of the line.

Add a slash before the $ to escape it, so it's used as a literal character.

--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 here. 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/97b9fd3b-0139-442b-8cb5-9722c26b3802n%40googlegroups.com.

Christopher Stone

unread,
May 25, 2021, 11:39:27 PM5/25/21
to BBEdit-Talk
On 05/25/2021, at 19:17, Duncan Thorne <dunc...@gmail.com> wrote:
I'm stumped when it comes to reformatting a text line that begins with a $ symbol. I want to replace the preceding line's line break, followed by the new line's $, with a tab-$.
For instance:
... 2021
$0.23

changed to:
... 2021 (tab) $0.23


Hey Duncan,

Look in Chapter 8 of BBEdit's User Manual: Searching with Grep (p182)

(Available from the BBEdit Help Menu.)

$ is a regex metacharacter referred to as an anchor, and it indicates the end of a line.

^ (the caret character) refers to the beginning of a line.

As Kerri mentions you escape these characters when using them literally.  (This takes a little getting used to.)

\ (backslash) is the usual escape character so – \$ and \^

Find:

\n^(\$\d+)


\n  == linefeed
^   == beginning of line
(   == start of capture group)
\$  == literal dollar sign character
\d  == digit
+   == one or more
)   == end of capture group

Replace:

\t\1


\t == literal tab
\1 == capture group 1

This is your basic regular expression.



A more advanced regular expression using a positive lookahead assertion:

Find:

\n(?=\$)


\n  == linefeed
(   == start of a NON-capture group
?=  == lookahead assertion which looks for a string without selecting it
\$  == literal dollar sign
)   == close of the non-capture group

Replace:

\t


\t == literal tab

With this method and your use-case I don't have to capture any text and put it back; I can just replace the found text with a tab.

Many people think regular expression are cruel and unusual punishment, but I enjoy them – they're fun (and sometimes also frustrating) puzzles to solve.  😎

--
Best Regards,
Chris

@lbutlr

unread,
May 26, 2021, 8:30:39 AM5/26/21
to BBEdit Talk
I won't repeat what everyone else has said, but a few other notes.

As a shortcut, whenever you see a RED characters in the bbedit search field that character is special, and throwing a \ in front of it will turn the pair blue, which means it will not be the special grep character but instead an escaped character.

\r is an escape for "return" character
\n which is an escape for a "newline" character

In nearly all BBEdit files, if not all, these are equivalent. This is not the case in all editors however.

If you go to the Search Menu and then "Pattern Playground" you will see threee items at the top next to the words "Search patter:" one looks like a g, one looks like a clock, and only looks like a ? In a circle. The last will show a long list of sample patters.

These same items appear int eh find/replace dialog, but the pattern playground is an excellent place to see what the expressions you are typing will match in the document, so I recommend starting there. It is very easy to then use the pattern for a find and replace.



--
When the routine bites hard / and ambitions are low And the
resentment rides high / but emotions won't grow And we're
changing our ways, / taking different roads Then love, love will
tear us apart again

Duncan Thorne

unread,
May 26, 2021, 8:45:56 AM5/26/21
to BBEdit Talk
You are all a helpful bunch but I'm still sinking! I looked at the manual and it addles my brain. Grep, zero-width positions, etc. Yikes. My limited Applescript background has been mainly with the ancient 32-bit Tex-Edit Plus, which was somewhere between a text editor and a word processor (fonts!) and even I could vaguely understand it. BBEdit is obviously more for programmers, and I'm not used to the deep end. In any event with much help from you patient folks in the community I've got a BBEdit script that would work well for my simple needs — except with $ signs.
I've found that /$ doesn't seem to do anything, and \$ throws up a syntax error. (Expected “"” but found unknown token.) But perhaps I'm meant to use one form in the "find" part another in "replace"?
Here's a sample line from my script where things go wrong:

tell front text window's text

replace "2021

\$" using " \$" options {search mode:grep, case sensitive:false, starting at top:true} -- the idea being to take out a line break and replace it with a tab.

end tell


Much appreciation!

Rich Siegel

unread,
May 26, 2021, 9:12:21 AM5/26/21
to BBEdit Talk
On 26 May 2021, at 0:19, Duncan Thorne wrote:

> You are all a helpful bunch but I'm still sinking! I looked at the
> manual
> and it addles my brain. Grep, zero-width positions, etc. Yikes.

I think you'll find it simpler if you eliminate Grep from the process
and do a literal search. Try writing the "replace" operation in your
script as follows:

replace "2021\\n$" using "2021\\t$" options {search mode:literal, case
sensitive:false, starting at top:true}

("\n" and "\t" are backslash character escapes for line break and tab,
respectively. The AppleScript editor will compile these into literal
characters, so to avoid this we add an extra backslash. If you don't do
that, the operation will still work, but the script is a little harder
to read.)

R.

--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <https://www.barebones.com/>

Someday I'll look back on all this and laugh... until they sedate me.

Duncan Thorne

unread,
May 26, 2021, 12:02:17 PM5/26/21
to BBEdit Talk
It works! Also, much tidier. Thanks Rich for that, and to everyone else for all the hand holding. I can't thank you enough.

Christopher Stone

unread,
May 26, 2021, 6:30:08 PM5/26/21
to BBEdit-Talk
On 05/25/2021, at 23:19, Duncan Thorne <dunc...@gmail.com> wrote:
Here's a sample line from my script where things go wrong:

tell front text window's text

replace "2021

\$" using " \$" options {search mode:grep, case sensitive:false, starting at top:true} -- the idea being to take out a line break and replace it with a tab.

end tell



Hey Duncan,

Backslash is the escape character in AppleScript, so when you need to write it as a literal you have to escape the escape character.

set myDollarSign to "\\$"

I know this is often quite confusing to neophyte AppleScripters, so please forgive me for forgetting to mention it explicitly.

For future references here's how you'd write the regular expression in AppleScript:

tell application "BBEdit"
    tell front document
        replace "(2021)\\n(\\$)" using "\\1\\t\\2" options {search mode:grep, case sensitive:false, starting at top:true}
    end tell
end tell

If you take the literal strings (between the quotes) and replace the "\\" with "\" as a literal string in BBEdit you'll get unescaped version that BBEdit wants.

A perhaps simpler trick to see the literal string is to copy it to the clipboard like so:

set the clipboard to "(2021)\\n(\\$)"

--> (2021)\n(\$)

The part after “--> ” is the pasted result.

“--> ” is a writing convention for indicating the result of an AppleScript.

I too am a long time user of Tex-Edit Plus and will sorely miss it down the road.


--
Best Regards,
Chris

Duncan Thorne

unread,
May 27, 2021, 12:17:28 PM5/27/21
to BBEdit Talk
Hi Chris,
I just noticed that you too use T-E+. I sing the praises of it every day and it's a big reason I'm still at Mojave, the last 32-bit compatible version of the OS. I wonder what happened to Tom Bender, the guy behind it. I've found no answer on Google.
Cheers,
Duncan

Christopher Stone

unread,
May 27, 2021, 7:50:57 PM5/27/21
to BBEdit-Talk
On 05/26/2021, at 21:42, Duncan Thorne <dunc...@gmail.com> wrote:
I just noticed that you too use T-E+. I sing the praises of it every day and it's a big reason I'm still at Mojave, the last 32-bit compatible version of the OS. I wonder what happened to Tom Bender, the guy behind it. I've found no answer on Google.


Hey Duncan,

I sent Tom a Christmas card in 2018 and had a brief email correspondence with him.

It's the usual story – he got a job, had a family, and got busy...  :-)

In my opinion the only really viable replacement for Tex-Edit Plus is Jedit Ω.

It doesn't have quite as organic a feel as Tex-Edit Plus, but it's similarly powerful and relatively simple.


--
Take Care,
Chris

Duncan Thorne

unread,
May 28, 2021, 12:45:49 PM5/28/21
to bbe...@googlegroups.com
Hi Chris,

Many thanks for the tip on Jedit Ω (used the Tex-Edit Plus special chars popup for "Ω"). I've downloaded the Jedit web edition and  will be poking around in it. So, from Texas to Japan. Of course you risk questions about using Applescript with it! Not sure the BBedit forum would be thrilled. So far I’ve managed to tell Jedit Ω to activate. A start. I notice $20 Canadian to unlock the full find capabilities. Does it allow wildcard searches (and handle $ in the friendly scripting-for-the-rest-of-us Tex-Edit way), I wonder.

Great that Tom got a family, etc. and I wish him well — although it’s a pity he didn’t convert T-E+ to 64-bit first 🤪. Priorities!

Cheers,
Duncan


--
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 here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to a topic in the Google Groups "BBEdit Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bbedit/xoorTiOkSCo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/1A9EF0CB-9150-4936-9E11-3B331BAEA1A3%40gmail.com.

Patrick Woolsey

unread,
May 28, 2021, 12:48:25 PM5/28/21
to bbe...@googlegroups.com
Good afternoon folks and just a gentle reminder that this thread
is drifting rather far afield for relevance... ;-)


Regards

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>




On 5/28/21 at 12:34 PM, dunc...@gmail.com (Duncan Thorne) wrote:

>Hi Chris,
>
>Many thanks for the tip on Jedit Ω (used the Tex-Edit Plus special chars popup for "Ω").

[... details elided ...]

Reply all
Reply to author
Forward
0 new messages