VCL using local variable in regular expression



I want to replace regexp used several times in my vcl by a local variable.

declare local var.regexp STRING;
set var.regexp = "^(BE|FR|NL)$";

if (client.geo.country_code ~ var.regexp) {
  # do stuff

But I have an error :

Syntax error: Expected `STRING` expression
at: (input Line 937 Pos 31)
if (client.geo.country_code ~ var.regexp) {

Is it possible to do regexp with local var ?




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.


Great !

Thanks Andrew !