Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
Apr 19, 2011

question about :matches_regex

Hi,met trouble in matches_regex now

 

 

difference in expressions below

 

 

set uri [HTTP::path]

 

 

{$uri matches_regex "^/(cjj)"} { pool cjj01-BRO }

 

 

{$uri matches_regex "^/cjj"} { pool cjj01-WSE }

 

 

 

If I wanna visit http://10.1.1.1/cjjtext&hh1 so the &uri should be cjjtext&hh1

 

 

1:Will this uri match ^/cjj or another one

 

2:which uri will match "^/(cjj)" ,actually I don't understand the () here

 

 

Can someone help me in this issue
  • Hi,

    I think both of those regexes would have the same exact matching. The parentheses would capture a match into a backreference which wouldn't be usable. If you wanted to match a literal ( or ), I think you'd need to escape them with backslashes.

    Regardless, if you're just trying to check if the URI starts wtih /cjj or /(cjj), you could use a string pattern instead. This would be significantly more efficient than a regex.

    
    switch -glob [HTTP::path] {
       "/(cjj)*" { pool cjj01-BRO }
       "/cjj*" { pool cjj01-WSE }
    }
    

    Aaron
  • Thanks for your answer

     

    "The parentheses would capture a match into a backreference which wouldn't be usable. "

     

     

    can you give me an example to make me understand this better

     

    you mean a variable?

     

    like $cjj ,and this cjj is set an value at the begining of the irule

     

    ($cjj)
  • Normally you could use a capturing group defined by the expression in the parentheses to capture the matched characters. Here's an example:

     

     

    regexp -inline {test([0-9]} "test321654654" original match

     

     

    $original would be set to the original string of test321654654 and $match would be set to 321654654. See the TCL wiki page for regexp for details: http://www.tcl.tk/man/tcl8.4/TclCmd/regexp.htm

     

     

    In your example, matches_regex doesn't support saving the matches. And it's much more efficient to use a string function to do the HTTP path checking anyhow.

     

     

    Aaron
  • oh

     

    got it

     

     

    It is not me who use this matches_regex

     

     

    Another engineer use this and I need to maintanian this irule ,but confused by ()

     

    So you mean we use a capturing group by the expression in () to capture the matched characters

     

     

    for example ([0-9]) test321654654

     

     

    so 321654654 will be captured to $match

     

     

    and if I use () in matches_regex ,because matches_regex doesn't support saving the matches,so it is useless.it will only work like a normal character without ()

     

     

    I mean matches_regex will ignore () in my case

     

     

    right?

     

  • That's it exactly. I'd still suggest using a switch statement instead of a regex.

     

     

    Aaron
  • we will ,haha

     

     

    but this irule is commerical ,should replace it later

     

     

    so matches_regex will cost more bigip resource?like CPU or memory?

     

     

    right?
  • it is so late in USA,you are still up

     

    haha

     

     

    you have high spirits in respecting work
  • matches_regex will cost more in CPU than a string function like switch.

     

     

    Aaron