Regular Expression evaluation in SuperCard - RegEx

93 views
Skip to first unread message

André Tremblay

unread,
May 31, 2018, 9:22:29 PM5/31/18
to SuperCard Discussion
Hello, 

I am looking for a way to evaluate RegEx in SC, any suggestions? 

Exemple(s) with AppleScript or a Shell would be welcome!

Regards

André Tremblay
PhotoGraphex

codegreen

unread,
May 31, 2018, 11:21:40 PM5/31/18
to superca...@googlegroups.com
On Thursday, May 31, 2018 at 9:22:29 PM UTC-4, André Tremblay wrote:
I am looking for a way to evaluate RegEx in SC, any suggestions? 

That's rather a broad question...

If you simply mean evaluate a string against a regex, you could do that using unixTask and the bash =~ operator:

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


Or are you trying use a regex with something like grep to filter text?

If so you can do it like this:

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


Or are you trying to do something else entirely?

-Mark

codegreen

unread,
Jun 1, 2018, 12:00:14 AM6/1/18
to superca...@googlegroups.com
That first example would probably be less overwhelming like this:

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


The idea was to show that =~ could handle pretty complex expressions, but I think I mighta buried the lede... ;-)

-Mark

André Tremblay

unread,
Jun 1, 2018, 10:27:10 PM6/1/18
to SuperCard Discussion

That's rather a broad question...

Sorry! I could have been more specific. In the conversion of my project I am trying to emulate an HyperCard XCFN :

RegMatch(Target,RegExpr)

for which I don't have information, otherwise than it seems that line 1 of the result would be a boolean for a match and that line 2 would be some text, probably the text resulting from the match. 

Your example would be fine if I could get a boolean for the match in line 1 of the result and the matching text in line 2 of the result. 

Regards

André
 

André Tremblay

unread,
Jun 3, 2018, 8:05:10 PM6/3/18
to SuperCard Discussion


Le vendredi 1 juin 2018 00:00:14 UTC-4, codegreen a écrit :
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... ;-)


Hello Mark, 

I was able to do some testing on OS X Tiger with the original RegMatch(Target,RegExpr) XFCN to evaluate the results with different values. 

By modifying your example, I came with this emulation for testing purpose that seems to work for the time being:

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


codegreen

unread,
Jun 4, 2018, 4:32:36 PM6/4/18
to SuperCard Discussion
Hi André,

Sorry, I was lost in Snow Leopard all weekend (from which I can't access the list) stripping cruft from the 4.8 source. I'm glad to see you managed to get this working on your own...

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


HTH,
-Mark

André Tremblay

unread,
Jun 4, 2018, 7:44:51 PM6/4/18
to SuperCard Discussion


Le lundi 4 juin 2018 16:32:36 UTC-4, codegreen a écrit :
 
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


You read in my mind! I'll use it! Thanks a bunch!

André  
Reply all
Reply to author
Forward
0 new messages