Community discussions

MikroTik App
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Mikrotik script and regex

Wed Apr 28, 2021 10:25 am

Hello there!

Probably i have a lot of problem with my knowledge, but here is my next question. I wanna use regular regular expression in my script, but i don't understand why isn't it work for me.

I used a few way them. First of all i don't escaped the dot so it was something like this:
address~"^(10.100.[1-9]{1,3}.[023456789]{1,3})$"
but its so far from where i need to be, so i ask for help, than i got this solution:
address~^(10[.]100[.][1-9]{1,3}[.](?:[02-9]|1\d){1,3})
Here i got errors for ? and for the \d , so they need to be escaped, but how? *divine spark* with double \ .
Sooo
address~"10\\.100\\.([1-9]\\d*|\\d{2,})\\.([^1]\$|\\d{2,})\$"
But in this case give me back a null value as variable, but like in this https://regex101.com/r/X6gdmW/1 site, probably it's must to work isn't it?

I hope someone will know what is the problem with it. Thx
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Mikrotik script and regex

Wed Apr 28, 2021 10:57 am

Hello there!

Probably i have a lot of problem with my knowledge, but here is my next question. I wanna use regular regular expression in my script, but i don't understand why isn't it work for me.

I used a few way them. First of all i don't escaped the dot so it was something like this:
address~"^(10.100.[1-9]{1,3}.[023456789]{1,3})$"
but its so far from where i need to be, so i ask for help, than i got this solution:
address~^(10[.]100[.][1-9]{1,3}[.](?:[02-9]|1\d){1,3})
Here i got errors for ? and for the \d , so they need to be escaped, but how? *divine spark* with double \ .
Sooo
address~"10\\.100\\.([1-9]\\d*|\\d{2,})\\.([^1]\$|\\d{2,})\$"
But in this case give me back a null value as variable, but like in this https://regex101.com/r/X6gdmW/1 site, probably it's must to work isn't it?

I hope someone will know what is the problem with it. Thx


https://wiki.mikrotik.com/wiki/Manual:R ... xpressions

https://pubs.opengroup.org/onlinepubs/9 ... hap09.html

address~"^(10.100.[1-9]{1,3}.[023456789]{1,3})$"
=>
address~"^10\.100\.[1-9]{1,3}\.[02-9]{1,3}$"

\d => [0-9]
Last edited by rextended on Wed Apr 28, 2021 11:02 am, edited 1 time in total.
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Re: Mikrotik script and regex

Wed Apr 28, 2021 11:01 am


address~"^(10.100.[1-9]{1,3}.[023456789]{1,3})$"
=>
address~"^10\.100\.[1-9]{1,3}\.[023456789]{1,3}$"
But, if i use only one backslash than it got an error.

Probably i used this code now:
if ([:len [/ip address find where address~"10.100."]] > 0)
do={:put "1";
if ([:len [/ip address find where address~"^(10\\.100\\.([1-9]|\\d{2,3})\\.([02-9]|\\d{2,}))\$"]]>0)
do={:put "2";:local var [/ip address find address~"^(10\\.100\\.[1-9]{1,3}\\.(\?:[02-9]|\\d{2,3})\$)"]; :put $var}}
Its just a test function, but in the end i want to use it in a lot of different router. But now i have this ip addresses in the test router:
10.100.0.4/24
10.100.1.11/24
10.100.44.1/24
The rules:
In the 3rd octet can't be a single 0, but it can be 10 or 100 etc.
and in the 4rd octet cant be a single 1 like in the 3rd example.
I need to search ip address with this 2 rules. So in the solution i need to save in a variable the ip address like 10.100.1.11(/24) and i want to use it later.
The first if statement is true so it give back the "1" but in the 2nd if statement gives back a null value.
Last edited by Fulmo on Wed Apr 28, 2021 11:13 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Mikrotik script and regex

Wed Apr 28, 2021 11:06 am


address~"^(10.100.[1-9]{1,3}.[023456789]{1,3})$"
=>
address~"^10\.100\.[1-9]{1,3}\.[023456789]{1,3}$"
But, if i use only one backslash than it got an error

On terminal you mus "manually" escape... the escape,

\ => \\

address~"^10\\.100\\.[1-9]{1,3}\\.[02-9]{1,3}\$"

also with \ before $

$ = \$
\d => [0-9]

? must be escaped on terminal: \?
 
Fulmo
just joined
Topic Author
Posts: 15
Joined: Mon Feb 22, 2021 12:32 pm

Re: Mikrotik script and regex

Wed Apr 28, 2021 11:16 am

\d => [0-9]

? must be escaped on terminal: \?
So mikrotik script doesn't operate with \d so i need to use [0-9] ?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Mikrotik script and regex

Wed Apr 28, 2021 11:34 am

\d => [0-9]

? must be escaped on terminal: \?
So mikrotik script doesn't operate with \d so i need to use [0-9] ?

better put [0-9] than count everytime how many \ you put on testing purpose or in saved script...

Who is online

Users browsing this forum: No registered users and 23 guests