Forum Discussion

jay_41157's avatar
jay_41157
Icon for Nimbostratus rankNimbostratus
Dec 17, 2008

double wildcard match for uri patterns

Hi,

 

 

Is it possible to use 2 wildcards when checking for a uri ?

 

 

For ex,

 

 

I want to look for

 

 

uri1abcURI2zzz

 

uri1xxxURI2wqq

 

 

so for the above can I do....

 

 

uri1*xxxURI2*

 

 

maybe with reg expressions ?

 

 

 

Thanks,

 

Jay

10 Replies

  • Hi Jay,

    You can use string match with multiple wildcards:

     
     string match -nocase "*pattern*" "some string" 
     

    Or you could use the -glob flag for switch:

     
     switch -glob [string tolower [HTTP::uri]] { 
        "uri1*xxxURI2*" { 
           ... 
     

    Either string-based option would be more efficient than a regex.

    Aaron
  • Aaron,

     

     

    Thanks for the response. However I am unclear on the syntax...

     

     

    how does the below look?

     

     

    if { [HTTP::uri] equals string match -nocase "portal/aaa/org/memberapp?*pageLabel=application*" } {

     

    HTTP::redirect http://redirecttotaget

     

    }

     

     

     

     

    All I have a total of 3 patterns that i will need to check for... so should I just use or statements ? or try and integrate this with a matchclass I had run into problems using a wildcard with a match class in the past.

     

     

     

    Thanks

     

     

  • If you have multiple patterns you want to check, a switch statement would help keep them organized:

     

     

       
       when HTTP_REQUEST {   
          
           Check requested URI   
          switch -glob [HTTP::uri] {   
             "*pattern1* -   
             "*pattern2* -   
             "*pattern3* {   
                 Execute this code for the first three patterns   
                HTTP::redirect "http://first_redirect.example.com"   
             }   
             "*pattern4* -   
             "*pattern5* {   
                 Execute this code for the next two patterns   
                HTTP::redirect "http://second_redirect.example.com"   
             }   
             default {   
                 Execute this code for the any cases that haven't been matched   
                HTTP::redirect "http://default_redirect.example.com"   
             }   
          }   
       }   
       

     

     

    Else, if you wanted to use a single if statement you could do something like this:

     

     

       
       when HTTP_REQUEST {   
          
           Check requested URI   
          if { ([string match -nocase "portal/aaa/org/memberapp?*pageLabel=application*" [HTTP::uri] ]) \   
             or ([string match -nocase "*pattern2*" [HTTP::uri]]) \   
             or ([string match -nocase "*pattern3*" [HTTP::uri]])}{   
          
             log local0. "[IP::client_addr]:[TCP::client_port]: Matched check for [HTTP::uri]"   
             HTTP::redirect http://redirecttotaget   
          }   
       }   
       

     

     

    Aaron
  • great, thanks. which option is more efficient in the irule ?

     

     

    Thanks
  • I've read in the forums and an old article (Click here) that an if statement is more efficient than a switch for a handful of comparisons. After that a switch is more efficient. At some point (around 100 elements) a datagroup would be more efficient than a switch. I haven't actually tested this before though.

     

     

    I'd guess the difference isn't huge for a handful of comparisons. I think it's more important that you use a form that's easy for you to read.

     

     

    If speed/resource usage is critical for you, you can test the versions using the timing command (Click here).

     

     

    Aaron
  • I dont think for this specific change we are at the point where speed / resource usage is an issues since this will be in place only for a couple of hours.

     

     

    I was curious if one was better over the other.

     

     

    Thanks for your help.
  • if i use:

     

     

    or ([string match -nocase [HTTP::uri] "/portal/acs/corg/memberapp?jay"]) that works, however if I use:

     

     

     

    or ([string match -nocase [HTTP::uri] "/portal/acs/corg/memberapp?*jay=*"])}{ -- seems like the * does not work as a wildcard...

     

    any thoughts ?

     

     

    thanks

     

     

  • the 1st or the second wild card neither of them are matched.

     

     

    /portal/acs/corg/memberapp?*jay=*

     

     

    I tried to use the below uri to test:

     

     

    /portal/acs/corg/memberapp?111jay=222; this does not match, I try to swap the *'s individually and same results. I will try to use \ for escaping ?

     

     

    Thanks
  • using:

     

     

    or ([string match -nocase [HTTP::uri] "/portal/acs/corg/memberapp/jay?*"])}{

     

     

    I get the * to work as a wild card.. what I need is to have a string append at the end of the wild card .
  • Sorry... I had the pattern and string reversed in my last post. It should be:

     

     

    string match -nocase $pattern $string_to_compare

     

     

    Aaron