Extracting capturing group contents in Varnish reg

2019-08-02 02:40发布

问题:

I have the following regular expression in Varnish configuration language

^/abc/([a-zA-Z0-9\-\ ]*)-([0-9]+)

Now , I want fetch the value $2 part(i.e [0-9]+) of regular expression in Varnish.

How can I get this value?

回答1:

You may use regsub in this case:

set req.url = regsub(req.url, "^/abc/([a-zA-Z0-9 -]*)-([0-9]+).*", "\2");

You match the whole string, capture the part you need, and replace with the appropriate backreference.