Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get text between two substrings



"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:
http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden

References: 
 >Get text between two substrings (From: Joseph Weaks <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.