Fwd: A Better Regex
Fwd: A Better Regex
- Subject: Fwd: A Better Regex
- From: T&B <email@hidden>
- Date: Mon, 20 Aug 2007 09:32:03 +1000
Hmmm so it seems that the Satimage OSAX doesn't do full PCRE regexp
(eg greedy matching and escape character for whitespace). So the best
AppleScript RegExp solution I have is my GetRegexMatches subroutine
below. Questions:
1. Can any perl scripters out there optimize my routine, such as
performing the ReplaceText operations inside perl? Obviously we still
want to be able to call it from AppleScript.
2. Is there a better/faster way to perform RegExp from AppleScript
that supports the full PCRE spec?
Thanks,
Tom
on run -- just a test
set regexString to ".*?(cd).*?(g.*?k).*?first\\s+([a-z]*)"
set searchInString to "abcdefghijklm is the first half of the
alphabet"
GetRegexMatches of regexString into searchInString given
modifiersString:"gis"
end run
-- test gives: {"cd", "ghijk", "half"}
on GetRegexMatches of regexString into searchInString given
modifiersString:modifiersString
if regexString is "" then
-- match all
set resultList to {searchInString}
else if regexString is null or searchInString is null then
set resultList to null
else
set arraySeparator to ascii number 1 -- arbitrary delimiter
-- need to do backslashing in perl script for better speed,
but for now:
set searchInString to (ReplaceText of searchInString by "\\\
\" instead of "\\")
set searchInString to (ReplaceText of searchInString by "\\'"
instead of "'")
set regexString to (ReplaceText of regexString by "\\'"
instead of "'")
set regexString to (ReplaceText of regexString by "\\/"
instead of "/")
set perlScript to "
$searchInString = '" & searchInString & "';
$regexString = '" & regexString & "';
$arraySeparator = '" & arraySeparator & "';
@regexResults = $searchInString =~ /$regexString/" & modifiersString
& ";" & "
print join( $arraySeparator, @regexResults );"
set perlResult to DoPerlScript(perlScript)
if perlResult is "" then
set resultList to {}
else
set text item delimiters to arraySeparator
set resultList to text items in perlResult
end if
end if
return resultList
end GetRegexMatches
on ReplaceText of textBlock instead of oldString by newString
if textBlock contains oldString then
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to oldString
set parsedList to text items of textBlock
set AppleScript's text item delimiters to newString
set textBlock to parsedList as text
set AppleScript's text item delimiters to oldDelimiters
end if
return textBlock
end ReplaceText
on DoPerlScript(perlScript)
set perlHeader to "#!/usr/bin/perl
"
set shellScript to "perl -e " & quoted form of (perlHeader &
perlScript)
try
set perlResult to do shell script shellScript
on error errorMessage
error "Perl error:" & errorMessage
end try
return perlResult
end DoPerlScript
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden