Need Help with MikroTik Script for Matching and Storing Patterns

Hi everyone,

I’m working on a MikroTik script and could use some help. My goal is to check if a command contains any of several patterns like “add name=”, “edit name=”, or “delete name=”. If a match is found, I want to store the matched pattern in a variable called matchedPattern and then log this variable.

Here’s the script I have so far:

:local command "just test"
:local patterns {"add name="; "edit name="; "delete name="; "disable name="; "enable name="}
:local matchedPattern ""

# Determine which pattern the command matches
:foreach pattern in=$patterns do={
    :if ([:find $command $pattern] != "") do={
        :set matchedPattern $pattern
    }
}
:log info $matchedPattern

However, when I test it, the log shows all the patterns (“add name=”, “edit name=”, “delete name=”, “disable name=”, and “enable name=”) even if the $command doesn’t match any of them.

I think I might be doing something wrong, but I’m not sure what. Could anyone help me correct this script or guide me on how to achieve the desired functionality?

Thanks in advance for your assistance!

the find result everytime is != “”
or is a number, or is nil
you must check only if the find return a number, and that number is the index of the string in the array.

So the result is a number of the index of the string so how to edit my script to achieve what i said in the post?

http://forum.mikrotik.com/t/searching-for-words-in-an-array/171425/1

Not sure “pattern matching” a config file is a good approach to any problem. The order of attributes can change, etc. etc.
So it be a complex endeavor even if you get past this one syntax issue. Just my opinion.

But what underlying problem are you trying to solve?