I'm working on some code that generates PCRE patterns used to ban objects in varnish and having a problem with varnish telling me the pattern is invalid.
This is one of the patterns generated by my code
(?i)(((^| )page\-2[^ ]*($| )))
And this is what varnish is telling me
0 Debug - "REGEX: <missing )>"
13 RxRequest c BAN
13 RxURL c /
13 RxProtocol c HTTP/1.1
13 RxHeader c User-Agent: curl/7.34.0
13 RxHeader c Accept: */*
13 RxHeader c Host: www.test.local
13 RxHeader c X-Tags: (?i)(((^| )page\-2[^ ]*($| )))
I've even tried simplifying the pattern during testing to this
page-2[^ ]
page\-2[^ ]
And I get this
0 Debug - "REGEX: <missing terminating ] for character class>"
A simple pattern like this works
page-2.*
I'm running varnish-3.0.2 revision cbf1284
The relevant VCL is
if (req.request == "BAN") {
ban("obj.http.X-Tags ~ " + req.http.X-Tags);
error 200 "Banned";
}
And, just so it's clear that the original pattern is valid

So apparently, as I accidentally discovered in my comment on the original question, varnish doesn't like space literals in regex. The solution in this case is to replace the space literal with \s, which matches on any whitespace character, including spaces, tabs, and newlines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With