Regex Format in Conditional DNS forwarding

Good to hear. You do kinda have to test them, since RouterOS has its own flavor. I kinda knew the ?! was likely not going work here.

But \D \w etc stuff I’m not sure about… and seemingly the 3rd problem here (1st being the lack of escaped “$”, 2nd the “?!” does not work) since OP using the [A-z] style…

While those work “standalone”, RouterOS does not seem to process them in a character class. Maybe this right in some flavor, but these should be “true” IMO:

:put ("a"~"[\\D]")   
# false
:put ("a"~"[\\w]") 
# false

Matching without the char-class , and just char-class-escape, they do work:

:put ("a"~"\\w")  
true
:put ("a"~"\\D") 
true

I generally use the [A-z] or [^A-z] style - since I think it’s more readable. But I would have thought “[\w]” would work…

I already pulled my hair out because of this limited POSIX regex implemention of ROS. In my case, make it very hard to impossible to write a regex for FWD for any subdomain except not matching 2 explicit 3rd Level Domains. Easy with negative lookahead, but Mikrotik likes us to suffer.

Do you think is it a bug in RB implementation?


([^a-zA-Z0-9][^-][^0-9]{2})(\.[\.]?ad\.localdomain$)

I’m exhausted from testing different possibilities. Can you see the error of not being able to validate just the domain?

:put (“ad.localdomain”~“([^a-zA-Z0-9][^-][^0-9]{2})(\.[\.]?ad\.localdomain$)”)
false

The above rule works in others testers that I made, just RB doesn’t recognize it.

But it’s working with: test.ad.localdomain and test-01.ad.localdomain



This is my first time using MK regex… I think I finally solve this puzzle.

As I have a script updating dns static on dhcp leasing, I need to make a small change. Previously I has DNS static using this domain: WORD-XX.ad.localdomain. The machine/printer name still the same, for example: SALES-01, SALES-02, etc., but I was made a change in the domain. Now all my lan computers will following this new standard: WORD-XX.LAN.AD.LOCALDOMAIN.

So I have changed the FWD regex to: (\.*[^lan|^wlan]\.ad\.localdomain$|^ad\.localdomain$)

Important: to debug, I have to use \ on terminal… but on regex you have to remove ALL \ and \ to get it working.

So, just use: .*[^lan|^wlan].ad.localdomain$|^ad.localdomain$


Any request to ad.localdomain or subdomains.ad.localdomain will be forwarded to AD DNS, except records with lan and wlan in the host, that in my case, represents clients using lan (cabled and wifi) that must be forwarded to MK.

I also implemented a DNS check in netwatch that, in case of timeout of my AD, I just disable the static DNS regex.

Apparently, now everything is running as a sharm.

Well, that does make it easy and likely okay. But “.” still means any character to the regex. While improbable, lan1ad.localdomain would match the domain parts. Not say it’s critical, but \. is there for a reason, since you want match the literal “.”.

:put ("a1com"~"a\\.com") 
# false
:put ("a1com"~"a.com")  
# true

Now the side-effect of [\w] not working… is this does work to match the literal . dot:

:put ("a.com"~"a[.]com") 
# true
:put ("a1com"~"a[.]com") 
# false

To the “ripping your hair out” commentary, apparently inside the [character class], the . is literal.. which might be right, IDK. But that avoid needing escaping things between RouterOS and some other regex parser, like regex101.com.