REGEX

I have the following test in a script:

:if ($"SPEED"~"384k/1M" && !($"NAME"~".*HT*" || $"NAME"~".*CAS*" ) ) do=

This correctly matches when the script name is:
HTxxx
xxHTxxx

And other combinations. It ALSO matches for ANY name that includes a capital “H”. It does NOT match a lower case “h”, even when combined with a “t”. It does NOT match an upper case “T”, unless there is ALSO an upper case “H” in the name. I need it to match uppercase HT only (as well as the “CAS” test, but that one seems to work for the most part). What am I missing in the REGEX test?

The regex should match capital “HT” only, and from a quick test on my router, that does seem to be the case:

[admin@MikroTik] > :global NAME "HT"
[admin@MikroTik] > :put ($"NAME"~".*HT*")
true

The regex, as written, should match
H
HT
xH
xHT
xHTT
xHx
Hx

(where “x” can be one or more of any character, including lowercase “h” or “t”)

Basically, it matches any string that contains an upper case “H” somewhere in it.

I realize that is what is matches. That is the issue. What I NEED it to match is a NAME that contains “HT” in the string. What am I missing?

Ah. Sorry. I was left with the impression that “HT” is not matched, and you wanted to include it in the match.

Well then… If you don’t want to match “H” only, then what you’re missing is a “.” in front of the second “*”, i.e. the regex should be “.HT.”.

That would match any string that contains the substring “HT”, including just the string “HT” itself, but would not match the strings “H” or “T”.

The “*” affects the character before it, and having it in front of “T” meant that you’re accepting zero or more “T” characters, not a zero or more of any character, which is what the “.” is.


Actually, come to think of it, because the pattern is not anchored in any way, you may as well just use “HT” as a pattern, i.e. make that line

:if ($"SPEED"~"384k/1M" && !($"NAME"~"HT" || $"NAME"~"CAS" ) ) do=

Basically, unless the pattern starts with “^”, it’s as if you have an implied “." at the start, and unless the pattern ends with “$”, it’s as if you have an implied ".” at the end.