nginx的位置正则表达式 - 字符类和火柴的范围(nginx location regex - c

2019-08-31 23:35发布

我想设置的路径正则表达式/s/<4-6 character string here> ,我捕捉到4-6字符串当作$ 1。

我试着用以下两个条目,但都失败

location ~ ^/s/([0-9a-zA-Z]){4,6}+$ { ...

location ~ ^/s/([0-9a-zA-Z]{4,6})+$ { ...

第一个想出了“未知的指令”和第二想出了“pcre_compile()失败:缺少)”

编辑

以下航线将由该位置服务:

/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)

下面的路线不会受到此位置服务:

/s/1
/s/12
/s/123
/s/a
/s/ab
/s/abc
/s/abc1234
/s/12345678

等等...

Answer 1:

如果你想捕捉4到6个字符,你为什么不已经把捕获括号内的量词?

类似的东西可能:

location ~ "^/s/([0-9a-zA-Z]{4,6})$" {...

( - 维基nginx的<)大括号都在正则表达式和块控制使用时,必须用引号(单人或双人),附上您的正则表达式



文章来源: nginx location regex - character class and range of matches
标签: regex nginx pcre