12-14-2023 07:25 AM
So I am trying to parse all the whitespace out of a string like: "gig1/0/1 - 2 , gig 1/0/5 , 6, 8, 9"
I used an online regexp checker to come up with the following search string:
Does exactly what I want. But when I try to use this same search string in an eem regexp statement, it matches nothing:
event manager applet test5
event none
action 010 set source_string "gig1/0/1 - 2 , gig 1/0/5 , 6, 8, 9"
action 020 set dest_string "NULL"
action 030 regexp "[\S]*" "$source_string" dest_string
action 040 puts "Dest = $dest_string"
#event manager run test5
Dest = NULL
So I guess "\S" (match everthing but whitespace) isn't supported. OK, so how about [^\s] (NOT match whitespace):
event manager applet test5
event none
action 010 set source_string "gig1/0/1 - 2 , gig 1/0/5 , 6, 8, 9"
action 020 set dest_string "NULL"
action 030 regexp "[^\s]*" "$source_string" dest_string
action 040 puts "Dest = $dest_string"
That gets closer:
#event manager run test5
Dest = gig1/0/1
But it still only catches the first whitespace character Based on the output from the online regexp checker, I'm expecting:
gig1/0/1-2,gig1/0/5,6,8,9
I've tried many combinations of () and * in the search string without success. Any ideas what I'm missing here??
Solved! Go to Solution.
12-14-2023 09:48 AM - edited 12-14-2023 09:55 AM
event manager applet test5
event none
action 010 set source_string "gig1/0/1 - 2 , gig 1/0/5 , 6, 8, 9"
action 020 foreach word "$source_string"
action 030 string trim "$word" "[\s]+"
action 040 append dest_string "$word"
action 050 end
action 060 puts "$dest_string"
INET#event manager run test5
gig1/0/1-2,gig1/0/5,6,8,9
12-14-2023 07:27 AM - edited 12-14-2023 07:27 AM
Hello @6502
Use curly braces {} to define the regular expression. This could helps to avoid potential issues with backslashes.
=> action 030 regexp {[\S]+} "$source_string" dest_string
12-14-2023 07:42 AM
M02@rt37 , thanks for the suggestion, but unfortunately introducing the curly braces gets me back to the applet returning NULL.
12-14-2023 07:55 AM
Ok @6502
you try simply action 030 regexp " " "$source_string" dest_string
12-14-2023 08:36 AM
That just returns a string with a single whitespace character. "[^$q $q]*" (where $q = a single " character) returns up to just before the first space character. regexp seems to like \s (vs \S), so I expected " " to yield the same failed results.
Based on the fact that my original [\S]* & its [^\s]* variant works in the regexp simulator, I'm guessing there's something that I don't understand about the eem "action regexp" that's causing it to not work as expected.
I can work around it with this script:
event manager applet test6
event none
action 10 set source_string "gig1/0/1 - 2 , gig1/0/5, gig1/0/7, gig1/0/9"
action 20 set dest_string ""
action 30 set substring ""
action 40 foreach substring "$source_string" " "
action 50 set dest_string "$dest_string$substring"
action 60 end
action 70 puts "Dest = $dest_string"
But the fact that I can't figure out the regexp and have to use all this extra code is vexing.
12-14-2023 09:48 AM - edited 12-14-2023 09:55 AM
event manager applet test5
event none
action 010 set source_string "gig1/0/1 - 2 , gig 1/0/5 , 6, 8, 9"
action 020 foreach word "$source_string"
action 030 string trim "$word" "[\s]+"
action 040 append dest_string "$word"
action 050 end
action 060 puts "$dest_string"
INET#event manager run test5
gig1/0/1-2,gig1/0/5,6,8,9
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide