matches_regex


Description

Tests if one string matches a regular expression.
Note: It is more efficient to use a string command versus a regular expression. Most scenarios can be handled using string match or scan .

Syntax

<string1> matches_regex <regex>

<string1> matches_regex <regex>

  • Tests if string1 matches the regular expression.

Examples

To match only hostnames starting with “www.” and ending with “.com”:

when HTTP_REQUEST {
  if {[string tolower [HTTP::host]] matches_regex {www\.\w+\.com} } {
     pool aol_pool
  } else {
     pool all_pool
 }
}

Example of the above using scan instead

when HTTP_REQUEST {
   if {[scan [string tolower [HTTP::host]] {www.%[a-z0-9.:-].com} host_token] == 1} {
      log local0. "Matched www.X.com check with match: $host_token"
      pool first_pool
   } else {
      pool second_pool
   }
}

To match any URI containing fx1, fx2, fx3 or fx4::
when HTTP_REQUEST {
  if {[HTTP::uri] matches_regex {.*fx[1234].*}} {
    pool fxpool
  }
}

OR

when HTTP_REQUEST {
  if {[HTTP::uri] matches_regex ".*fx\[1234\].*"} {
    pool fxpool
  }
}

Example using string match:
when HTTP_REQUEST {
  if {[string match {*fx[1234]*} [HTTP::uri]]}{
    pool fxpool
  } else {
    pool second_pool
  }
}