Regex positional capture in map

map’s documentation
http://nginx.org/en/docs/http/ngx_http_map_module.html#map states:
A regular expression can contain named and positional captures that can
later be used in other directives along with the resulting variable.

Trying to do the following failed validation:
map $host $foo {
“~^www.(.)$” $1; # Positional capture fails
default $foo;
}

What am I doing wrong?

B. R.

Using named captures works (as demonstrated by
redirect - Nginx extract a value from a variable or any string - Stack Overflow),
though:
map $host $foo {
“~^www.(?.)$” $domain; # Named capture wins
default $foo;
}

B. R.

On 03 Nov 2014, at 17:18, B.R. [email protected] wrote:

Using named captures works (as demonstrated by
redirect - Nginx extract a value from a variable or any string - Stack Overflow),
though:
map $host $foo {
“~^www.(?.)$” $domain; # Named capture wins
default $foo;
}

default $host;


Igor S.

On Mon, Nov 3, 2014 at 3:22 PM, Igor S. [email protected] wrote:

​Yup, my bad: typo while writing the example map.​
It does not interfere much with the problem though. Any piece of advice?
:o)


*B. R.*​

On Mon, Nov 03, 2014 at 03:03:22PM +0100, B.R. wrote:

Hi there,

I think this is a documentation bug.

map’s documentation
http://nginx.org/en/docs/http/ngx_http_map_module.html#map states:
A regular expression can contain named and positional captures that can
later be used in other directives along with the resulting variable.

Strictly, those words are true – you can use the named or positional
captures in other directives. It does not say that you can use them in
this same directive. (Or at least, it is possible to read it that way,
if you really wanted to.)

But practically, it should state that the resulting value can be a
string,
or can be a variable or a named capture, but cannot be a positional
capture. (Or whatever the actual restrictions are.)

f

Francis D. [email protected]

Thanks to both of you!
Documentation bug, then…

B. R.

Hello!

On Mon, Nov 03, 2014 at 11:02:23PM +0100, B.R. wrote:

default $host;

​Yup, my bad: typo while writing the example map.​
It does not interfere much with the problem though. Any piece of advice? :o)

As you already found yourself, map{} doesn’t allow to use
positional captures in resulting values. Use named captures
instead.


Maxim D.
http://nginx.org/