Forum Discussion

lstewart_53611's avatar
lstewart_53611
Icon for Nimbostratus rankNimbostratus
Apr 29, 2010

regex error - help?

I'm getting an error when trying to use the matches_regex operator. I think I might be using regexes that would work in perl but not in TCL. Can anyone see what's wrong with this? This is at the end of an iRule which works fine without this final elseif, and my brackets are actually balanced in the full rule: elseif {[HTTP::host] == "hostwearelookingat.com" } { if { [HTTP::uri] contains "/1/Kingdoms/users/"} {

 

set urivar

 

if { ! ( $urivar matches_regex ^.*?

 

\/1\/Kingdoms\/users/\[a-zA-Z0-9]) } {

 

pool kingdoms-reserved

 

}

 

else {

 

pool prod-platform

 

}

 

}

 

 

 

When I try this, I get the error:

 

 

line 38: [parse error: PARSE syntax 989 {syntax error in expression " ! ( $urivar matches_regex .*?\/1\/Kingdoms\/users/\[a-zA-Z0...": character not legal in expressions}] [{ ! ( $urivar matches_regex .*?\/1\/Kingdoms\/users/\[a-zA-Z0-9]) }]

 

 

The purpose of the rule is to match a uri of /1/Kingdoms/users/(any alphanumeric) and NOT match anything else. If someone simply has another suggestion of how to do that more simply, I'll take that, too. But I'm not sure what the issue is with my expression above. Thanks in advance.

3 Replies

  • Additionally, I apologize for my newbish lack of indentation. I'll go find out how to do that right posthaste.
  • Try wrapping the regex in curly braces to prevent the square braces in the character class from being interpreted as commands. Also, I don't think you don't need to escape the forward slashes in this.

     

     

    if { ! ( $urivar matches_regex {^.*?/1/Kingdoms/users/[a-zA-Z0-9]} ) } {

     

     

    That said, I think you can do this much more efficiently with a scan command:

     

     

    http://www.tcl.tk/man/tcl8.4/TclCmd/scan.htm

     

     

    Aaron
  • Ah, that was it. Thanks so much. I was afraid there was some big TCL regex difference of which I was unaware. Thank you!