VCL using local variable in regular expression
Hello,
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 ?
Regards
Mathieu
-
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. -
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
Cheers
Please sign in to leave a comment.
Comments
4 comments