gpt4 book ai didi

regex - nginx 位置正则表达式 - 字符类和匹配范围

转载 作者:行者123 更新时间:2023-12-04 04:47:51 33 4
gpt4 key购买 nike

我正在尝试为路径 /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() failed: missing )”

编辑

此位置将提供以下路线:
/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

等等...

最佳答案

如果要捕获 4 到 6 个字符,为什么不将量词放在捕获括号内?

也许是这样的:

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

花括号用于正则表达式和块控制,您必须用引号(单引号或双引号)括住正则表达式(<-- wiki nginx)

关于regex - nginx 位置正则表达式 - 字符类和匹配范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16175665/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com