• 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: Capturing parts of text string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Capturing parts of text string


  • Subject: Re: Capturing parts of text string
  • From: has <email@hidden>
  • Date: Mon, 4 Jul 2005 09:35:17 +0100

Patrick Collins wrote:

>Just curious if I have a string:
>
>set x to "Las Vegas, Nevada 89102"
>
>how is the best way to extract the city, state, and zip into their own
>strings?

Regular expressions are absolutely ideal for this sort of thing. Example (uses TextCommands <http://freespace.virgin.net/hamish.sanderson/index.html#textcommands>):

-------

set txt to "Las Vegas, Nevada 89102
Northbrook, IL 60062
Las Cruces, New Mexico 85123-1234"

tell application "TextCommands"
    set txt to change linebreaks txt to Unix format
    search txt for "^(.*?), (.*?) ([-0-9]+)$" with regex and individual line matching
end tell
--> {{"Las Vegas", "Nevada", "89102"}, {"Northbrook", "IL", "60062"}, {"Las Cruces", "New Mexico", "85123-1234"}}

-------

You might need to finesse the search pattern a bit depending exactly on how your input data is presented, but this shouldn't be hard. If you've not used regular expressions before, there's plenty of tutorials available online and in book form.

HTH

has
--
http://freespace.virgin.net/hamish.sanderson/
 _______________________________________________
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

  • Prev by Date: Re: Capturing parts of text string
  • Next by Date: Quark's print command is broken on Tiger!
  • Previous by thread: Re: Capturing parts of text string
  • Next by thread: Re: Capturing parts of text string
  • Index(es):
    • Date
    • Thread