3

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 Regular expression visualization

Lee Saferite
  • 3,124
  • 22
  • 30

1 Answers1

2

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.

RevanProdigalKnight
  • 1,316
  • 1
  • 14
  • 23