How to extract first n characters of a string in varnish-vcl?

1.2k views Asked by At

I'm looking for a way to extract the first 'n' characters from a string in VCL. I couldn't find any function like trim(str,starting_pos) or substring(str,len) in the VCL documentation. I've tried searching for this on google and stackoverflow and nothing came up, so I'm asking here. I appreciate your help.

1

There are 1 answers

0
Koen Rouwhorst On BEST ANSWER

I'm not aware of any such string functions being available in the Fastly Varnish environment.

However, I think you could accomplish the same using regular expression capture groups.

set req.http.Foo = "foobar";

if (req.http.Foo ~ "^(.{0,3})") {
  set resp.http.match0 = re.group.0; # this should now equal 'foo'
}