Forum Discussion

Robert_47833's avatar
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 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

8 Replies

  • 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 }

  • 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


  • 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:



    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.



  • 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 will only work like a normal character without ()



    I mean matches_regex will ignore () in my case





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



  • we will ,haha



    but this irule is commerical ,should replace it later



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



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





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