Which reminds me, on the choice of which language to reach for when a task arises, that this morning I wanted a function that would give a true/false value in exchange for a Regex pattern and a string:
regexTest :: RegexPattern -> String -> Bool
My choices were either AppleScript:
use framework "Foundation" -- "OS X" Yosemite onwards, for NSRegularExpression
-- regexTest :: RegexPattern -> String -> Bool
on regexTest(strRegex, str)
set ca to current application
set oString to ca's NSString's stringWithString:str
((ca's NSRegularExpression's regularExpressionWithPattern:strRegex ¬
options:((ca's NSRegularExpressionAnchorsMatchLines as integer)) ¬
|error|:(missing value))'s firstMatchInString:oString options:0 ¬
range:{location:0, |length|:oString's |length|()}) is not missing value
end regexTest
regexTest("\\s", tab)
-- result: true
or JavaScript for Automation:
// regexTest :: RegexPattern -> String -> Bool
function regexTest (rgx, s) {
return rgx.test(s);
}
regexTest(/\s/, '\t');
// result: true
AppleScript seemed a bit hard for a person of my advancing years …