Applescript returning \1 from grep search

85 views
Skip to first unread message

jgill

unread,
Sep 30, 2016, 5:53:39 PM9/30/16
to BBEdit Talk


I need to extract the date from <h3>September 30th</h3> in a HTML document

I can search using - set theMonth to find "<h3>\\s*(.*?)\\s*</h3>" searching in text 1 of text document "index.html" options {search mode:grep} with selecting match

How can I access the \1 reference inside the brackets in Applescript?

(I do know the long way to parse it but there must be something simpler?)

Christopher Stone

unread,
Oct 1, 2016, 11:53:18 AM10/1/16
to BBEdit-Talk
On Sep 30, 2016, at 16:53, jgill <joegille...@gmail.com> wrote:
I need to extract the date from <h3>September 30th</h3> in a HTML document

I can search using - set theMonth to find "<h3>\\s*(.*?)\\s*</h3>" searching in text 1 of text document "index.html" options {search mode:grep} with selecting match

How can I access the \1 reference inside the brackets in Applescript?


Hey Joe,

As far as I know you can't in find operations, although you can in replace operations.

Find with selecting found string is a poor way to do a replace – it's slow and cumbersome – although it can be the only way to accomplish jobs under some circumstances.

The better way is to use replace (if it works for that job):

-------------------------------------------------------------------------------------------
tell application "BBEdit"
   replace "MATCH" using "REPLACE" options {search mode:grep, case sensitive:false, starting at top:true}
end tell
-------------------------------------------------------------------------------------------

Otherwise you may have to do something like this:

-------------------------------------------------------------------------------------------
tell application "BBEdit"
   tell front document
      set foundTextRecord to find "<h3>\\s*(.*?)\\s*</h3>" options {search mode:grep, starting at top:true} with selecting match
      set foundText to found text of foundTextRecord
      set parsedText to replace "<h3>\\s*(.*?)\\s*</h3>" searchingString foundText using "\\1" options {search mode:grep, case sensitive:false, starting at top:true}
   end tell
end tell
-------------------------------------------------------------------------------------------

And that's why I generally prefer to use the Satimage.osax for find and find/replace operations.

-------------------------------------------------------------------------------------------
# Requires the Satimage.osax
-------------------------------------------------------------------------------------------

set _text to bbeditFrontWinText()

try
   set parsedText to find text "<h3>\\s*(.*?)\\s*</h3>" in _text using "\\1" with regexp and string result without all occurrences
on error
   set parsedText to false
end try

if parsedTextfalse then
   # DO SOMETHING
end if

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on bbeditFrontWinText()
   tell application "BBEdit"
      tell front document to its text
   end tell
end bbeditFrontWinText
-------------------------------------------------------------------------------------------

Although I will often turn to a Perl, awk, or sed text filter when it's efficient to do so.

--
Best Regards,
Chris

jgill

unread,
Oct 1, 2016, 4:51:23 PM10/1/16
to BBEdit Talk
As always, thank you Christopher. It never occurred to me to use replace because I wasn't replacing anything ;?)

Rich Siegel

unread,
Oct 1, 2016, 5:33:16 PM10/1/16
to bbe...@googlegroups.com
On Friday, September 30, 2016, jgill <joegille...@gmail.com> wrote:

> I can search using - set theMonth to find "<h3>\\s*(.*?)\\s*</h3>"
> searching in text 1 of text document "index.html" options {search
> mode:grep} with selecting match
>
> How can I access the \1 reference inside the brackets in Applescript?

The "grep substitution" verb should do that, e.g. after the "find" succeeds,

grep substitution of "\\1"

should return the text represented by the first capture group.

Enjoy,

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

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

Christopher Stone

unread,
Oct 2, 2016, 9:18:25 PM10/2/16
to BBEdit-Talk
On Oct 01, 2016, at 16:33, Rich Siegel <sie...@barebones.com> wrote:
The "grep substitution" verb should do that, e.g. after the "find" succeeds,


Grrf...  I was looking for that and couldn't find it.  :)

--
Take Care,
Chris

Reply all
Reply to author
Forward
0 new messages