Regular expressions: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
(→‎{{header|AppleScript}}: Updated with shell script and ASObjC examples.)
Line 165: Line 165:
return message
return message
end try</lang>
end try</lang>
----
As from macOS 10.14 Mojave, third-party scripting additions (OSAXen) such as the Satimage OSAX are essentially unusable. (One of Apple's increasingly tight security measures.) They ''are'' allowed under very strict conditions as part of an application's own resources and [https://latenightsw.com Late Night Software], the developer of Script Debugger, has released a [https://forum.latenightsw.com/t/use-satimage-scripting-additions-on-catalina-and-mojave/1576 SatimageOSAX] application as a stop-gap measure to allow existing Satimage-dependent scripts to be used with minimal editing until they're rewritten to use other commands.

The alternatives at the moment are to use one of the text-editing languages available through the <tt>do shell script</tt> command (AppleScript's own StandardAdditions OSAX does still work) or to use AppleScriptObjectiveC. The scripts below assume it's known that the strings will consist of just one line.

'''do shell script:'''
<lang applescript>-- Get the run of non-white-space at the end, if any.
try
set output to (do shell script "echo 'I am a string' | egrep -o '\\S+$'")
on error message
set output to "No match"
end try
-- Replace the first instance of "orig…" with "modified".
set moreOutput to(do shell script "echo 'I am the original string' | sed 's/orig[a-z]*/modified/'")
return output & linefeed & moreOutput</lang>

'''ASObjC''' uses [http://userguide.icu-project.org/strings/regexp ICU regex]:
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

-- Get the run of non-white-space at the end, if any.
set aString to current application's class "NSString"'s stringWithString:("I am a string")
set matchRange to aString's rangeOfString:("\\S++$") ¬
options:(current application's NSRegularExpressionSearch) range:({0, aString's |length|()})
if (matchRange's |length|() > 0) then
set output to aString's substringWithRange:(matchRange)
else
set output to "No match"
end if

-- Replace the first instance of "orig…" with "modified".
set anotherString to current application's class "NSString"'s stringWithString:("I am the original string")
set matchRange2 to anotherString's rangeOfString:("orig[a-z]*+") ¬
options:(current application's NSRegularExpressionSearch) range:({0, anotherString's |length|()})
if (matchRange2's |length|() > 0) then
set moreOutput to anotherString's stringByReplacingCharactersInRange:(matchRange2) withString:("modified")
else
set moreOutput to anotherString
end if

return (output as text) & linefeed & moreOutput</lang>
As an alternative to the NSString regex options used above, there's also a dedicated NSRegularExpression class:
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

-- Get the run of non-white-space at the end, if any.
set aString to current application's class "NSString"'s stringWithString:("I am a string")
set aRegex to current application's class "NSRegularExpression"'s regularExpressionWithPattern:("\\S++$") options:(0) |error|:(missing value)
set matchRange to aRegex's rangeOfFirstMatchInString:(aString) options:(0) range:({0, aString's |length|()})
if (matchRange's |length|() > 0) then
set output to aString's substringWithRange:(matchRange)
else
set output to "No match"
end if

-- Replace the first instance of "orig…" with "modified".
set anotherString to current application's class "NSString"'s stringWithString:("I am the original string")
set anotherRegex to current application's class "NSRegularExpression"'s regularExpressionWithPattern:("orig[a-z]*+") options:(0) |error|:(missing value)
set matchRange2 to anotherRegex's rangeOfFirstMatchInString:(anotherString) options:(0) range:({0, anotherString's |length|()})
if (matchRange2's |length|() > 0) then
set moreOutput to anotherString's stringByReplacingCharactersInRange:(matchRange2) withString:("modified")
else
set moreOutput to anotherString
end if

return (output as text) & linefeed & moreOutput</lang>


{{out}}
{{out}}
The latter three scripts all return:
<pre>
</pre>
<pre>"string
I am the modified string"</pre>

Shane Stanley has released a script library called [http://www.macosxautomation.com/applescript/apps/Script_Libs.html#RegexAndStuffLib RegexAndStuffLib], which is written in ASObjC, but provides less verbose commands for use in client scripts.


=={{header|Argile}}==
=={{header|Argile}}==