• 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
Re: Get text between two substrings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get text between two substrings


  • Subject: Re: Get text between two substrings
  • From: "Gary (Lists)" <email@hidden>
  • Date: Thu, 30 Jun 2005 04:11:06 -0400

"Joseph Weaks" wrote:

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

A non-optimized, format-dependent, not very robust PCRE [Perl-compatible
reg-ex] (tested in TextWrangler) to grab an HTML comment could be:

Find:     >([^<]*)!
Replace:  \1

> Would someone recommend a pure Applescript method (offset or TIDs)?

If you will only be concerned about getting the text inside HTMl comments,
then you can forgo the variability of a regular expression -- the form and
sequence are known so you can get to the data easily.

One pure AS (TID-based) means of retrieving the content of an HTML comment
tag could be:

to split(needle, haystack)
    set oTIDs to the text item delimiters
    set the text item delimiters to needle
    set hay to (text items of haystack)
    set the text item delimiters to oTIDs
    return hay
end split
set t to "<!-- Begin -->I really like pie<!-- End -->"
set commentString to item 1 of split({"<!--"}, item 2 of split({"-->"}, t))
--> "I really like pie"

> I've been using this handler I made, but I think it's slow:

> on trimContent(theString, theStart, theEnd)
[...]
> end trimContent

This work has been done for you, if you are interested in using AppleScript
libraries.  The 'String' library, available from
<http://applemods.sourceforge.net/mods/Data/index.php>, contains commands to
achieve these tasks.

For example, among others, there are:

    normaliseWhiteSpace(str) -- convert any tabs, linefeeds and returns to
spaces
        str : string
        Result : string

    removeExtraSpaces(str) -- convert multiple spaces to a single space
        str : string
        Result : string

    trimStart(str) -- trim any white space (space/tab/return/linefeed) from
the start of a string
        str : string
        Result : string

    trimEnd(str) -- trim any white space (space/tab/return/linefeed) from
the end of a string
        str : string
        Result : string

    trimBoth(str) -- trim any white space (space/tab/return/linefeed) from
both ends of a string
        str : string
        Result : string


Visit the Getting Started page for the installer, which comes with a handful
of the most commonly used libraries (this list has been recently updated.)

<http://applemods.sourceforge.net/getstarted.html>

The OS X base library bundle now includes:

# Date
# List
# Number
# String
# StringIO
# Types
# and Finder Extras

--
Gary

 _______________________________________________
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: Joseph Weaks <email@hidden>
References: 
 >Get text between two substrings (From: Joseph Weaks <email@hidden>)

  • Prev by Date: Get text between two substrings
  • Next by Date: Re: path to as string
  • Previous by thread: Get text between two substrings
  • Next by thread: Re: Get text between two substrings
  • Index(es):
    • Date
    • Thread