Unix command to evaluate RegEx while running OS X Tiger

31 views
Skip to first unread message

Collection PhotoGraphex

unread,
Aug 21, 2019, 9:49:31 PM8/21/19
to SuperCard Discussion

Hello, 


I know this is somewhat offtopic, but maybe the memory of a member of this forum can provide some guidance on this issue while I am trying to run a SuperCard project with OS X Tiger. 

Trying to run the project, that otherwise runs without this issue on OS X Snow Leopard and up, I am stumbling a Unix command to evaluate a RegEx expression. For this purpose, I am using the script proposed by Mark on this post: 


The actual script I am using is as follow: 

function RegMatch inText, inExpr -- 20180604 
  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

As an exemple, in this case: 

inText = "CO 2345

inExpr = "^(A|AA|B|BA|BC|BO|BR|C|CR|CM|CO|DA|DN|DM|DO|EO|EQ|EP|ER|ET|EX|HT|ST|ZT) +[0-9]+ ?[0-9]?[0-9]?[0-9]?[ABCDEFGHIJKL]?" 

After merging: 

regex = "txt="CO 2345";regex="^(A|AA|B|BA|BC|BO|BR|C|CR|CM|CO|DA|DN|DM|DO|EO|EQ|EP|ER|ET|EX|HT|ST|ZT) +[0-9]+ ?[0-9]?[0-9]?[0-9]?[ABCDEFGHIJKL]?";if [[ "$txt" =~ $regex ]];then echo "TRUE
"$BASH_REMATCH;else echo "FALSE";fi"

If I paste regex in Terminal in OS X High Sierra, I am getting this right result: 

TRUE
CO 1234

If I paste regex in Terminal in OS X Tiger, the command get mangled in this way: 

MiniBay:~ minibay$ txt="CO 1234";regex="^(A|AA|B|BA|BC|BO|BR|C|CR|CM|CO|DA|DN|DM|DO|EO|EQ|EP|ER|ET|EX|HT|ST|ZT) +[0-9]+ ?[0-9]?[0-9]?[0-9]?[ABCDEFGHIJKL]?";if [[ "$txt" =~ $regex ]];then echo "TRUE
-bash: conditional binary operator expected
-bash: syntax error near `=~'
MiniBay:~ minibay$ "$BASH_REMATCH;else echo "FALSE";fi

I wonder where is the error in the RegEx expression while being with OS X Tiger? 

I suppose it is not possible to have an expression that would be valid for OS X Tiger and up! 

But by using the SC function systemVersion(long), I can intercept the system version and put the right RegEx expression accordingly!

Maybe someone would have a clue?

Regards

André Tremblay
PhotoGraphex

codegreen

unread,
Aug 21, 2019, 10:48:40 PM8/21/19
to SuperCard Discussion
Unfortunately Tiger only supports so-called basic regular expressions (which don't recognize "?", "+", "{", "|", "(", or ")" as metachars).

You can usually work around "+" by using alternate syntax. For example [0-9]+ is equivalent to [0-9]{1,} or [0-9][0-9]*.

HTH,
-Mark

Collection PhotoGraphex

unread,
Aug 22, 2019, 3:38:40 PM8/22/19
to SuperCard Discussion
Hello Mark, 

Following your suggestion I started to look for litterature about RegEx and I looked back to the original HyperCard script, which is more than 25 years old, to see how the RegEx expression was written back then. I found out it was exactly the same! 

This is confirmed by this test in OS X Tiger Terminal, by typing only the RegEx command: 

txt="CO 1234";regex="^(A|AA|B|BA|BC|BO|BR|C|CR|CM|CO|DA|DN|DM|DO|EO|EQ|EP|ER|ET|EX|HT|ST|ZT) +[0-9]+ ?[0-9]?[0-9]?[0-9]?[ABCDEFGHIJKL]?", echo "$txt"

It does return in that case for the provided txt the expected result: CO 1234
 
It seems the issue is with the unix conditional command: =~

If I type in OS X Tiger Terminal, the full line, RegEx and the Unix conditional expression:

txt="CO 1234";regex="^(A|AA|B|BA|BC|BO|BR|C|CR|CM|CO|DA|DN|DM|DO|EO|EQ|EP|ER|ET|EX|HT|ST|ZT) +[0-9]+ ?[0-9]?[0-9]?[0-9]?[ABCDEFGHIJKL]?";if [[ "$txt" =~ $regex ]];then echo "TRUE"
$BASH_REMATCH;else echo "FALSE";fi

It returns: 

-bash: conditional binary operator expected
-bash: syntax error near `=~'

Would you have a clue how to solve it? 

Otherwise this conditional statement can be put out of Unix and evaluated with SuperTalk!

And why not the whole function can be rewritten in SuperTalk!

Regards

André Tremblay
PhotoGraphex

codegreen

unread,
Aug 22, 2019, 5:21:33 PM8/22/19
to SuperCard Discussion
LOL! OK then, so bash 2.x doesn't support the =~ operator either (which evidently was introduced in 3.0).

So basically you have two choices here: either you can rewrite your expression so it doesn't use =~ (which can be a pain), or you can update the version of bash you're running in Tiger via something like fink or homebrew (the usual workaround).

If you like you can even keep both versions installed (for backward compatibility) with a bit of extra space voodoo.

AFAIK OS X still uses bash 3 (released in 2007, it's at 5 something now) because 4.0 and up are issued under the GPL v3 license (which Apple doesn't want to be exposed to) so FWIW installing anything newer puts you in uncharted waters (though I'm not aware of any issues with later versions, I can't speak from experience here).

Normally this can be a bit of a pain long term because OS updates may overwrite yours and replace your new bash with the original, but I suspect Tiger won't be getting many more updates... ;-)

For detailed instructions, Google "brew install bash".

-Mark
Reply all
Reply to author
Forward
0 new messages