I am looking for a way to evaluate RegEx in SC, any suggestions?
on mouseUp
ask "Enter an email address to test:" with "st...@apple.com"
if it ≠ "" then answer validateEmail(it)
end mouseUp
function validateEmail theEmail
local proto = merge("email=`[[theEmail]]`[[cr]]regex=`^([A-Za-z]+[A-Za-z0-9]*((\.|\-|\_)?[A-Za-z]+[A-Za-z0-9]*){1,})" & ¬
"@(([A-Za-z]+[A-Za-z0-9]*)+((\.|\-|\_)?([A-Za-z]+[A-Za-z0-9]*)+){1,})+\.([A-Za-z]{2,})+`[[cr]]if « `$email` =~ $regex »;" && ¬
"then[[cr]]echo `$email looks valid`[[cr]]else[[cr]]echo `$email looks invalid`[[cr]]fi"), ¬
regex = replace(replace(proto, "«", "[["), "»", "]]")
return unixTask("", "/bin/bash", "-c", regex)
end validateEmail
on mouseUp
answer file "Select a file containing email addresses:"
if it ≠ "" then ask list shell(merge("grep -E -o `\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b` `[[hfsToPosix(it)]]` | sort | uniq -i"))
end mouseUp
function validateEmail theEmail
local leftBraces = "[[", rightBraces = "]]", ¬
regex = merge("email=`[[theEmail]]`[[cr]]regex=`^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6})`[[cr]]if [[leftBraces]]" && ¬
"`$email` =~ $regex [[rightBraces]]; then[[cr]]echo `$email looks valid`[[cr]]else[[cr]]echo `$email looks invalid`[[cr]]fi")
return unixTask("", "/bin/bash", "-c", regex)
end validateEmail
That's rather a broad question...
That first example would probably be less overwhelming like this:
The idea was to show that =~ could handle pretty complex expressions, but I think I mighta buried the lede... ;-)
function RegMatch SomeText,RegExpr -- 20180602
local leftBraces = "[[", rightBraces = "]]",¬
regex = merge("Texte=`[[SomeText]]`[[cr]]regex=`[[RegExpr]]`[[cr]]if [[leftBraces]]" && ¬
"`$Texte` =~ $regex [[rightBraces]]; then[[cr]]echo $BASH_REMATCH[[cr]]else[[cr]]echo `FALSE`[[cr]]fi")
get unixTask("", "/bin/bash", "-c", regex)
if it ≠ False then return TRUE&RETURN&it else return FALSE
end RegMatch
Regards
André Tremblay
PhotoGraphex
function RegMatch inText, inExpr
local leftBraces = "[[", rightBraces = "]]", regex = merge("txt=`[[inText]]`;regex=`[[inExpr]]`;if [[leftBraces]] `$txt` =~ $regex [[rightBraces]];then echo `TRUE[[cr]]`$BASH_REMATCH;else echo `FALSE`;fi")
return unixTask("", "/bin/bash", "-c", regex)
end RegMatch
Not that you asked, but FWIW here's how I'd write that:function RegMatch inText, inExpr
local leftBraces = "[[", rightBraces = "]]", regex = merge("txt=`[[inText]]`;regex=`[[inExpr]]`;if [[leftBraces]] `$txt` =~ $regex [[rightBraces]];then echo `TRUE[[cr]]`$BASH_REMATCH;else echo `FALSE`;fi")
return unixTask("", "/bin/bash", "-c", regex)
end RegMatch