Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? Regular expressions in TCC

Dec
238
2
I'm still back in the dark ages, using TCC v.20 (under Windows 10 Pro).

I've written a bazillion regular expressions over the years but rarely use them in TCC. I've never been clear about using regexes successfully in TCC. (Per the OPTION command, they're set to Perl.)

Code:
iff "%1" =~ "[abc]" then
    echo YES
endiff

It's a simple character class: "a" or "b" or "c". I get the "YES" result if the command-line argument is a single character ("a" or "b" or "c"). If the argument is something like "axyz" — no match is noted. Turns out, removing the quotation-marks from around "[abc]" fixes this. But it isn't case-sensitive. It should be. Can case-sensitivity be FORCED in these situations?

As for something like this:

Code:
iff "%1" =~ "a|b" then
    echo YES
endiff

It works if the command-line argument is a single character, "a" or "b" — but not if the argument is longer than one character unless the argument BEGINS with "a" or "b". But, I haven't specified "start of input" using "^" here.

And, "=~" requests a match. It doesn't mean "equals" or "is only" or "starts with." Removing the quotation-marks around "a|b" causes the error "unknown command ENDIFF" — so clearly that isn't the solution. Then, what is?
 
The target text and regex both include the quotes, which effectively anchor the beginning and end of the regex that is within them. Try
"%1" =~ ".*[abc].*"

For case-insensitivity try
"%i" =~ "(?-i:.*[abc].*)"
 
Thanks.
I was stuck thinking Perl-ish and didn't think it through enough. The matches I did get seem to have been case-insensitive by default, so I'd be using something like this for case-sensitivity:
"%1" =~ "(?i:.*[abc].*)"
 
(?i:...)
turns case insensitivity on (case sensitivity off).
(?-i:...)
turns case insensitivity off (case sensitivity on).
 
With "=~" you're testing for the (whole) string MATCHING the regex. "[abc]" and "[a|b]" describe a single character. Any string with more than one character will not match.

You can test for the string CONTAINING a match like this.
Code:
v:\> if %@regex["a|b",abc] == 1 echo yes
yes

v:\> if %@regex["a|b",def] == 1 echo yes

v:\>
 
With "=~" you're testing for the (whole) string MATCHING the regex. "[abc]" and "[a|b]" describe a single character. Any string with more than one character will not match.

It's not the =~ operator that's causing this; it's the quotes, which are included in the target text and the regex. The regex "[abc]" matches three characters, not one:
Code:
> if "axy" =~ "[abc]" echo yes

> if "axy" =~ [abc] echo yes
yes

> if axy =~ [abc] echo yes
yes
 
Then that's a bug. The quotes don't belong in either. Compare to @REGEX.
Code:
v:\> if %@regex[[abc],axy] == 1 echo yes
yes

v:\> if %@regex[[abc],"axy"] == 1 echo yes
yes

v:\> if %@regex["[abc]",axy] == 1 echo yes
yes

v:\> if %@regex["[abc]","axy"] == 1 echo yes
yes

The help says this about "=~".
string 1 matches the regular expression in string 2

Of @REGEX, it says
Returns 1 if the expression was found and 0 if it was not

They sound like two different things to me. If they're meant to be the same, that should be made clear. IMHO, the string "axy" does not match the regex "[abc]", while the expression "[abc]" can be found in the string "axy".
 

Similar threads

Back
Top