Regular Expressions and Smile
Regular Expressions and Smile
- Subject: Regular Expressions and Smile
- From: "Daniel A. Shockley" <email@hidden>
- Date: Thu, 10 Jan 2002 17:39:59 -0500
Hello all,
I'm trying to do regular expressions and running into some annoying
problems. Perhaps I'm missing some knowledge of Regular Expressions,
but I've run into a serious problem with RegEx by Lenoard Rosenthal.
The REReplace command uses the following dictionary entry:
REReplace : Replaces any substring that matches the pattern with the
replacement string
REReplace a list of string -- text to be matched
with string -- replacement text
pattern string -- regular expression pattern
[ignoreCase boolean] -- Should the match be done ignoring case?
[separator string] -- line separator character (default is
carriage return/CR)
Result : string -- the new version of the text
Does anyone else notice a major problem here?
'with' is a _reserved_ word in AppleScript. Interestingly enough, it
lets you use with as a keyword, which is all fine and good until you
want to include the option 'ignoreCase true.' When you compile that,
the compiler automatically "helps" you out by changing that into
'with ignoreCase' which works when you run it. However, if you then
make changes to the code at all, you get an error message: "Expected
expression but found parameter name." So, basically, it seems to be
impossible to use the ignoreCase option with REReplace unless you
never want to edit your script after the first compile. Then again,
you could go and fix that line every time you go to re-compile your
script, but I'm NOT doing that.
I had hoped to use RegEx OSAX instead of BBEdit's grep to speed up a
few scripts that make many RegEx search and replaces, but I'm running
into more and more problems like this. Satimage's RegEx functions
also don't work, as you can't use parentheses groups to get parts of
matches into the replacement string.
Any ideas?
Here's some sample code:
------------------
set myText to "John Smith
Jeff Matthewson
Robert Downing
JOHN Stephenson"
set newText to REReplace myText pattern "^John (.+)" with "\\1, John"
ignoreCase true
-------------
When you compile and run this, you get what you want. The script compiles to:
------
set myText to "John Smith
Jeff Matthewson
Robert Downing
JOHN Stephenson"
set newText to REReplace myText pattern "^John (.+)" with "\\1,
John" with ignoreCase
-------
and works, returning:
"Smith, John
Jeff Matthewson
Robert Downing
Stephenson, John"
If you leave out the ignoreCase, it won't change the JOHN Stephenson
line. Granted in this example you can add extra code to first
correctly capitalize the name, but that is just not possible for all
the places I've used BBEdit already.
However, now that the script has TWO 'with' commands, it will fail on
Syntax checking. This is bad, and I don't know of any way to keep
AppleScript from changing the form. Does anyone else?
Short of that, does anyone know a better or alternate way of doing
multiple fast greps in Mac OS 9 AppleScript? (I know I could use
other tools in Mac OS X, or even MacPerl)
--
----
Daniel A. Shockley
email@hidden
email@hidden
http://www.danshockley.com