5 Basic Tips to Using Regular Expressions (RegEx) in Varnish

5 Basic Tips to Using Regular Expressions (RegEx) in Varnish

Even though Varnish is an extremely popular tool out of the box, you will quickly find you will need to customize it for the intricacies of your site.

Whether it be passing search box results, allowing certain cookies, or returning various header responses, the use of regular expressions is prominent in VCL programming.

Varnish does have some quirkiness with its implementation of regular expressions, so we will cover the top five tips.

In Varnish, you can use regular expressions much like any other language. The most common usage is the vcl_recv function to match URLs. In general, you need to escape ,. ?, &, but not /.  This can be a point of confusion for some folks, so I will provide a few examples.

Varnish uses Perl-compatible regular expressions, so once you get through these basics, I recommend that you check out the main page for PCRE. Honestly, though, in most cases you will only need simple regular expressions in your VCL.

In addition, to develop and test your regular expressions, I suggest you use a tool such as https://regex101.com/.  This can dramatically speed up your development and reduce the number of errors.

1. Exactly Equal and Approximately Equal

The use of == and ~ determine whether to match the string exactly or match if the string appears anywhere in the term.  Note that the == is a strict comparison and does not use regular expressions, while ~ always uses regular expressions.

2. Use ^ to Begin a String and $ to End It

Sometimes you only  want to match at the beginning or end of a term (or both). Varnish makes this pretty straightforward as well.

3. Using | to Match Multiple Options

Using an OR (|) can provide a powerful method for combining many lines into one.

4.  Wildcard Matching

As with most regular expressions, it can be useful to use ?, *, and + to match characters or strings. This is fully supported by Varnish, too.

 5. Substitution via Regular Expression (regsub and regsuball)

There is no doubt that you will eventually need to replace some text in a term. Varnish has a function called regsub (and its cousin regsuball) that search for a string in a term and replace it with another string.

 Conclusion

Varnish makes the use of regular expressions quite simple, and with a little practice, this will become second nature to you. Of course, feel free to leave any questions below, and we will try to answer them as best as we can!

Ryan Frankel

Questions or Comments? Ask Ryan!

Ask a question and Ryan will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.

  • Ryan,

    Thanks for help!

    • frankel0

      No problem Fahad!

  • dfseGoat

    Is this up to date for varnish 4.0? Very hard to know which of the guides out there are current since varnish changed regex engines

    • frankel0

      This should be up-to-date and ready to rock for Varnish 4.0. If you have any trouble let us know!