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!

ABOUT THE AUTHOR
Ryan Frankel

Ryan Frankel has been a professional in the tech industry for more than 10 years and has been developing websites for more than 15. With his background in integrated circuit design and digital signal processing, he has a fundamental understanding of hardware systems and the software that runs them. Ryan now sits as the CTO of Digital Brands Inc. and manages all of the server infrastructure of their websites, as well as their development team. In addition, Ryan has a passion for guitars, good coffee, and puppies.