VCL using local variable in regular expression



  • Andrew Betts

    Hi Mathieu,

    Sadly not. I'd love to say that we support dynamic regexs but unfortunately they have to be string literals. In the case of this particular pattern, you're just testing for membership of a set, which I would recommend that you do with a table. You can do that like this:


    Outside of any subroutine / or in an 'init' snippet

    table countries { "BE": "Belgium", "FR": "France", "NL": "Netherlands" }

    In vcl_recv

    if (table.lookup(countries, client.geo.country_code)) { # do stuff } ```

    You can of course use the table.lookup function repeatedly to reference the same country list.

    Comment actions Permalink
  • messenjer

    Great !

    Thanks Andrew !

    Comment actions Permalink
  • Ichi

    This variable may works better on this case.

    declare local var.list-of-countries STRING;
    set var.list-of-countries = "BE, FR, NL";
    if (std.strstr(var.list-of-countries, client.geo.country_code)) {
     # do stuff

    I create a fiddle, please take a look at here


    Comment actions Permalink
  • messenjer

    Thanks, it's even easier that way !

    Comment actions Permalink

Please sign in to leave a comment.