Community discussions

MikroTik App
 
User avatar
akira463
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Thu Jan 11, 2018 12:30 pm
Location: Ph

How to check if special character is present in the message?

Wed Aug 23, 2023 5:39 am

Please do help , im having a hard time fixing my code, but still it does not work , i only want to check if the certain special character is present in the message
here's my code
:local message "!Your message here"
:local specialChars "!@#$%^&*"
:local found false

:foreach char in=$message do={
    :foreach special in=$specialChars do={
        :if ($char ~ $special) do={
            :set found true
            :log warning "found"
        }
    }
}
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to check if special character is present in the message?

Wed Aug 23, 2023 6:16 am

Strings are NOT arrays, so you cannot loop char-by-char in RouterOS script. You can however use a regex on the original string to do this check for "bad chars". The only issue is "$" is a special char to RouterOS, so the $ dollar-sign needs to be escaped with backslash like \$

In the most basic form, this works:
:put ("bad string!"~"[!@#\$%^&*]")

As a check, this "throws" an error if "bad string" – again the $ must be escaped since that mean a variable name is next in RouterOS...
:global messageText "bad string!!! & *even bad word d@c% or s#\$%"
:if ($messageText~"[!@#\$%^&*]") do={
    :error "bad char detected"
} 
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to check if special character is present in the message?

Wed Aug 23, 2023 6:20 am

So your code should be just:
{
  :local message "!Your message here"
  :local specialChars "[!@#\$%^&*]"
  :local found false

  :if ($message ~ $specialChars) do={
      :set found true
      :log warning "found"
  }
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to check if special character is present in the message?

Wed Aug 23, 2023 2:27 pm

A little function... just add your unwanted characters after \FF. If is "-" unwanted, must be put at the end.
For default \01-\19\7F-\FF check for any characters that RouterOS do not support.

Simply return true if is present at least one invalid charactes present on passed string, or false if no invalid character is found.
{
:local invalidchr do={:if ($1~"[\01-\19\7F-\FF\24\3F\\\60]") do={:return true} else={:return false}}
# on the example RegEx \24 is the Dollar symbol and \3F is the question mark, \\ is the \, and \60 are the ` (is not the ' )

:put [$invalidchr ("test invalid \$")]
}
 
User avatar
akira463
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Thu Jan 11, 2018 12:30 pm
Location: Ph

Re: How to check if special character is present in the message?

Wed Aug 23, 2023 4:10 pm

Thank you so much masters! now it works!!!

Who is online

Users browsing this forum: No registered users and 4 guests