nginx位置正则表达式-字符类别和匹配范围 [英] nginx location regex - character class and range of matches
问题描述
我正在尝试为路径/s/<4-6 character string here>
设置正则表达式,在其中将4-6字符串捕获为$ 1.
I am trying to setup a regex for the path /s/<4-6 character string here>
where I capture the 4-6 character string as $1.
我尝试使用以下两个条目,但都失败了
I tried using the following two entries, but both fail
location ~ ^/s/([0-9a-zA-Z]){4,6}+$ { ...
location ~ ^/s/([0-9a-zA-Z]{4,6})+$ { ...
第一个出现未知指令",第二个出现"pcre_compile()失败:缺少)"
The first one comes up with 'unknown directive' and the second comes up with 'pcre_compile() failed: missing )'
编辑
此位置将提供以下路线:
The following routes would be served by this location:
/s/1234 (and I would capture '1234' in $1)
/s/12345 (and I would capture '12345' in $1)
/s/123456 (and I would capture '123456' in $1)
/s/abcd (and I would capture 'abcd' in $1)
/s/abcde (and I would capture 'abcde' in $1)
/s/abcdef (and I would capture 'abcdef' in $1)
/s/a1b2c (and I would capture 'a1b2c' in $1)
此位置不提供以下路线:
The following routes would NOT be served by this location:
/s/1
/s/12
/s/123
/s/a
/s/ab
/s/abc
/s/abc1234
/s/12345678
等...
推荐答案
如果要捕获4到6个字符,为什么不将量词放在捕获括号内?
If you want to capture 4 to 6 characters, why you don't have put the quantifier inside the capture parenthesis?
类似的东西:
location ~ "^/s/([0-9a-zA-Z]{4,6})$" {...
在正则表达式和块控制中都使用大括号,您必须在正则表达式中使用双引号(单引号或双引号)(<-Wiki Nginx)
Curly braces are used both in regex and for block control, you must enclose your regex with quotes (single or double) (<-- wiki nginx)
这篇关于nginx位置正则表达式-字符类别和匹配范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!