Re: Find and Replace Text
Re: Find and Replace Text
- Subject: Re: Find and Replace Text
- From: has <email@hidden>
- Date: Thu, 25 Apr 2002 02:26:44 +0100
Eric Phillips wrote:
>
\What I want to change is when there is a 'Word,SpaceNumber'. I would like
>
the number in the next cell so I would like to change the ',Space' to a
>
'Tab'.
>
I was thinking about using something like MS Word and script it to do find
>
and replace. If I do it this way is there a way of searching for ', space
>
number' no matter what the number is and have it replace with 'tab number'
You can do this using regular expressions. AS doesn't have these built in
unfortunately, but there are various apps (e.g. BBEdit) and osaxen (RegEx
Commands, Satimage osax) that you can use. Implementations tend to vary a
bit, but the basic principles are the same and much of the syntax is pretty
consistent from one version to another.
Here's a simple example using the Satimage osax (OS8/9/X).:
======================================================================
set theString to "foo, bar, 123"
change ", ([[:digit:]])" into "\\t\\1" in theString with regexp
--> "foo, bar 123"
======================================================================
You can get an huge amount of control over text matching/replacing using
regular expressions, far more than you'll have with a basic
find-and-replace. Well worth learning about. Regex Commands (OS8/9) has a
very good manual [1] that is well worth reading even if you don't use that
particular osax.
>
or do I need to have nine find and replace statements to handle each
>
different integer
That would probably be the simplest method. A basic TID-based
find-and-replace handler will be quite adequate by the sounds of it. You
can find three variations on the theme in the stringLib library on my
website (link below). And here's an example of how you could use it:
======================================================================
property stringLib : load script alias "[your path to stringLib here]"
set theString to "foo, bar, 123"
tell stringLib to multiFindAndReplace(theString, {", 1", ", 2", ...},
[NO-BREAK]{" 1", " 2", ...}, {doSafely:false})
--> "foo, bar 123"
======================================================================
HTH
has
[1] The Regex Commands manual does have one unfortunate error that I know
of. On p8, the second-last paragraph should read:
REMatch someText pattern "^\d+[^0-9]+(\\d+)" using "\\1"
It also has one unfortunate omission: it completely forgets to mention that
the regex commands have a case-insensitive option. Check the osax's
dictionary for info on this.
Those niggles aside, I still recommend reading it (it's where I learnt
regular expressions). Even if you use the Satimage osax instead, it still
makes a good introduction.
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.