Forum Discussion

Amanpreet_Singh's avatar
Amanpreet_Singh
Icon for Cirrostratus rankCirrostratus
Sep 19, 2016

Irule with regex to match /

Hi everyone, Can anybody suggest how can I create a iRule with regex that matches Abc.com////allow or abc.com//allow// or I should say any number of forward slashes before and/or after allow

 

Basically I want to district a uri/page called allow to a specif group of IP which I already called in a data group.

 

Thx

 

1 Reply

  • Hi Amanpreet,

    the best approach would be to avoid RegEx pattern as much as possible. RegEx has a very poor performance compared to -glob matchings and most patterns can be easily expressed using the -glob syntax.

    Using -glob matching via if { [string match $x $y] }{script}

     

    if { [string match "*/allow/*" [HTTP::path]] } then {
         Do what ever you want with the path containing "*/allow/*"
    } else {
         Do what ever you want with the path not containing "*/allow/*"
    }
    

     

    Using -glob matching via switch -glob -- $x $y {script}

     

    switch -glob -- [HTTP::path] "*/allow/*" {
         Do what ever you want with the path containing "*/allow/*"
    } default {
         Do what ever you want with the path not containing "*/allow/*"
    }
    

     

    Well, if you still want to use -regex, then feel free to use one of the code samples below...

    Using -regex matching via if { $x matches_regex $y }{script}

     

    if { [HTTP::path] matches_regex ".*/allow/.*" } then {
         Do what ever you want with the path containing "*/allow/*"
    } else {
         Do what ever you want with the path not containing "*/allow/*"
    }
    

     

    Using -regex matching via switch -regex - $x $y {script}

     

    switch -regex -- [HTTP::path] ".*/allow/.*" {
         Do what ever you want with the path containing "*/allow/*"
    } default {
         Do what ever you want with the path not containing "*/allow/*"
    }
    

     

    Recommended Read: iRules 101 - 10 - Regular Expressions: Think twice, no three times, about using Regular Expressions

    https://devcentral.f5.com/articles/irules-101-10-regular-expressions

    Note: If you need to filter out xyz//allow/ or /allow//xyz or even xyz//allow//xyz but not xyz/allow/xyz then you could use a syntax as outlined below...

     

    if { ( [string match "*//allow/*" [HTTP::path]] ) or ( [string match "*/allow//*" [HTTP::path]] ) } then {
         Do what ever you want with the path containing "*//allow//*"
    } else {
         Do what ever you want with the path not containing "*//allow//*"
    }
    

     

    ... or using a RegEx version...

     

    if { [HTTP::path] matches_regex {.*(//allow/|/allow//).*} } then {
         Do what ever you want with the path containing "*//allow//*"
    } else {
         Do what ever you want with the path not containing "*//allow//*"  
    }
    

     

    Cheers, Kai