Search code examples
regexvarnish-vcl

Varnish Regular Expression for get variable array


I'm looking to extract get variable values from URLs inside of varnish. The get variable in question is named "source" and may or may not be an array. I would like to take the value(s) of source and comma separate them. Putting the URLs through my desired regex would yield the following:

  • t.com/?source=1
    • 1
  • t.com/?a=1&source=1
    • 1
  • t.com/?source[]=1
    • 1
  • t.com/?source[]=1&source[]=2
    • 1,2
  • t.com/?a=1&source[]=1&source[]=2
    • 1,2
  • t.com/?a=1&source[]=1&source[]=2&source[]=3
    • 1,2,3

I have created the regex that matches each of those cases appropriately, though I have little experience with capture groups for the purpose of replacement. Here is what I have constructed:

((\?|\&)source(\[])?=(?P<sources>[^&]+))+

Visualization: https://www.debuggex.com/i/_2ib6j-6VKTWE_vV.png


Solution

  • Since varnish has really limited functionalities (no array manipulation, loops, ...), a way to do this is to apply two regexes:

    1. The first will capture the source values and replace it with comma separated values

      use .*?[&?]source(?:\[\])?=([^&=\n]+) and replace with $1, => DEMO

    2. The second will remove the trailing comma

      use ,$ and replace with an empty string => DEMO