• 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: String searching
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String searching


  • Subject: Re: String searching
  • From: Graff <email@hidden>
  • Date: Mon, 19 Jan 2004 14:07:59 -0500

You could just do this:
------------
set theCode to "hi <a href> boo </a> bye"
set itemLocale to offset of "boo" in theCode

set reverseSearch to reverse of every character of "<a href" as string
set reverseText to (reverse of characters 1 thru (itemLocale - 1) of theCode as string)
set startTagOffset to (length of reverseSearch) + (offset of reverseSearch in reverseText)

set endTagOffset to offset of "/a>" in (text (itemLocale + 1) thru -1 of theCode)

display dialog "item locale: " & itemLocale
display dialog "Offset backwards: " & startTagOffset
display dialog "Offset forwards: " & endTagOffset
------------

Basically, find your word then make two backwards strings - one of your text up to your word and one of the string you want to search backwards for. Then find the offset of the reverse search string in the reverse text. This will give the END of the string you are searching backwards for, add in its length to find the front of it.

The forward find is easier since it just finds the offset of a string in the text beyond the start of the word you originally located.

- Ken

On Jan 19, 2004, at 12:29 PM, Steve Suranie wrote:

Does anyone have a snippet of code to do offset searches backwards and forwards from a certain point in a string?

For example if I have this code:

tell application "Internet Explorer"
Activate
OpenURL "http://edition.cnn.com/"; toWindow -1
with timeout of 3600 seconds
set theCode to (GetSource) as string
end timeout
set itemLocale to offset of "Pakistan" in theCode
end tell

the word pakistan appears at the 17,224 character in the string theCode - it's actually in this line

<a href=\"/2004/WORLD/asiapcf/01/18/pakistan.scientists.nukes/ index.html\">Pakistan widens nuke probe</a>

What I would like to be able to do is search backwards from itemLocale of theCode to find the first instance of "<a href" and then to search forward from that point to find the first instance of "</a>" Basically I want to be able to pull the hyperlink associated with the word I am searching for.

Or if there is a simplier way of doing it that would be appreciated as well.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

References: 
 >String searching (From: "Steve Suranie" <email@hidden>)

  • Prev by Date: Running ssh from AppleScript...
  • Next by Date: Re: Standard Additions 'read' command - basic questions
  • Previous by thread: Re: String searching
  • Next by thread: Running ssh from AppleScript...
  • Index(es):
    • Date
    • Thread