• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Get text between two substrings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Get text between two substrings


  • Subject: Get text between two substrings
  • From: Joseph Weaks <email@hidden>
  • Date: Thu, 30 Jun 2005 02:31:36 -0500

Given a string, what is a good do shell script one-liner to grab the text in between two sub-strings? In other words:

set theString to "<!-- Begin -->My very excellent mother just served us nine pies<!-- End -->"

set newString to do shell script "echo '" & theString & "' | perl something '<!-- Begin -->' something '<!-- End -->'"

-- My very excellent mother just served us nine pies


Would someone recommend a pure Applescript method (offset or TIDs)? I've been using this handler I made, but I think it's slow:


on trimContent(theString, theStart, theEnd)
-- if theStart or theEnd is an integer, then it trims at those characters, otherwise, it assumes each a string to be used as an offset
if (class of theStart) ≠ integer then
set theOffset to (offset of theStart in theString)
if theOffset = 0 then return 0
set theStart to (theOffset + (length of theStart))
end if

if (class of theEnd) ≠ integer then
set theOffset to (offset of theEnd in theString)
if theOffset = 0 then return 0
set theEnd to (theOffset - 1)
end if
set theString to text theStart thru theEnd of theString
end trimContent
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Re: Get text between two substrings
      • From: "Gary (Lists)" <email@hidden>
  • Prev by Date: Re: Dialog box suppression
  • Next by Date: Re: Get text between two substrings
  • Previous by thread: Re: Dialog box suppression
  • Next by thread: Re: Get text between two substrings
  • Index(es):
    • Date
    • Thread