cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
656
Views
3
Helpful
5
Replies

EEM: regexp not returning all matches

6502
Level 1
Level 1

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:

6502_0-1702566972426.png

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??

1 Accepted Solution

Accepted Solutions

Dan Frey
Cisco Employee
Cisco Employee

 

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

 

View solution in original post

5 Replies 5

M02@rt37
VIP
VIP

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

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

6502
Level 1
Level 1

M02@rt37 , thanks for the suggestion, but unfortunately introducing the curly braces gets me back to the applet returning NULL.

Ok @6502 

you try simply action 030 regexp " " "$source_string" dest_string

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

6502
Level 1
Level 1

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.

Dan Frey
Cisco Employee
Cisco Employee

 

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