2013-04-23 106 views
6

我想设置路径/s/<4-6 character string here>的正则表达式,其中我将4-6字符字符串捕获为$ 1。nginx位置正则表达式 - 字符类和匹配范围

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

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 

等等

回答

22

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

类似的东西可能:( - 维基nginx的<)

+0

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

花括号都在正则表达式和块控制使用时,必须用引号(单人或双人),附上您的正则表达式我想要捕获和限制字符串为4-6个字符。您在第一个4-6之后拥有无限个字符,使用'。*' – Dave 2013-04-23 19:01:58

+0

@Dave:您的请求不明确。请给出一个你想要治疗的url的具体例子。 – 2013-04-23 19:33:13

+0

我认为我的要求很明确。我想从'/ s/1234'或'abcde'从'/ s/abcde'中捕获'1234',除了4,5或6个字符串之外,不允许'/ s /'之后的任何内容。因此'/ s/x'或'/ s/xy'或'/ s/xyz'或'/ s/abcdxyz'将不会由此位置块提供服务。 – Dave 2013-04-23 19:39:44