Variables in regex expressions

Often when trying to solve a Problem A, you encounter a Problem B which takes on a life of its own. :slight_smile: This is such a case.

Is it possible to insert variables in VCL regex expressions? (as the regex pattern itself).

Checking the documentation:

VCL regular expression cheat sheet
Regular expressions in Fastly VCL | Fastly Documentation

Here’s what I’d suggest on both those pages.

Option 1:

Add a small footnote: “Note: It is not currently possible to use variables in regex expressions. A regex must be a string.”

or

Option 2:

Include examples in the docs covering the following situations, using variables in the regex: regsub(), a ~ b, variables concatenated with strings, variables concatenated with variables.

Relatable.

No. You clearly figured that out :slight_smile:

Agree we should document this. But actually it’s a slightly broader issue. The STRING type has two variants, a string constant which needs to have a known value at compile time, and a kind of string expression which can be composed at runtime from string variables or concatenation.

The regex pattern is an example of a compile time constant string.

What could be a good workaround when someone are facing this problem?

@rfraile depending on the scope and complexity of the problem you’re solving, looking into using our Compute product might be the best option. You’ll have access to a fully-featured programming language and all of the capabilities to match.