nginx正则表达式匹配多个可能性并将其连接到一个组中

huangapple go评论57阅读模式
英文:

nginx regex match with multiple possibility and concat in one group

问题

这是我的nginx正则表达式位置。

location ~ "^/live/midrate/v1/rates/((?:[A-Z]{6})-(?:SPT|ON|W[1-3]|M(?:[1-9][0-9]?)|Y[2-5])-(?:[DO])-(?:[ON]))$"

请求URI是

live/midrate/v1/rates/GBPUSD-SPT-D-O

结果是

找到匹配  捕获组 1: GBPUSD-SPT-D-O 2:  3:  4:  5:  6:

首先,我想要只有一个捕获组,而不是现在的多个空组,并且我需要支持逗号分隔的相同模式。

live/midrate/v1/rates/GBPUSD-SPT-D-O,EURUSD-M1-D-O,EURGP-SPT-O-O

响应应该是

找到匹配  捕获组 1: GBPUSD-SPT-D-O,EURUSD-M1-D-O,EURGBP-SPT-O-O
英文:

This is my regex location for nginx.

location ~ "^/live/midrate/v1/rates/((?:[A-Z]{6})-(?:SPT|ON|W[1-3]|M(?:[1-9][0-9]?)|Y[2-5])-(?:[DO])-(?:[ON]))$"

The request URI is

live/midrate/v1/rates/GBPUSD-SPT-D-O

and result is

Match found  Capture Groups 1: GBPUSD-SPT-D-O 2:  3:  4:  5:  6: 

firstly I want to have 1 group instead of multiple groups which are empty now and I need to support comma-separated with same pattern.

live/midrate/v1/rates/GBPUSD-SPT-D-O,EURUSD-M1-D-O,EURGP-SPT-O-O

and the response should be

Match found  Capture Groups 1: GBPUSD-SPT-D-O,EURUSD-M1-D-O,EURGBP-SPT-O-O

答案1

得分: 0

捕获逗号分隔的模式序列的一般规则是

(pat(?:,pat)*)

其中`pat`是单个迭代的模式。

所以在你的情况下,它会是:

^/live/midrate/v1/rates/((?:[A-Z]{6})-(?:SPT|ON|W[1-3]|M(?:[1-9][0-9]?)|Y[2-5])-(?:[DO])-(?:[ON])(?:,(?:[A-Z]{6})-(?:SPT|ON|W[1-3]|M(?:[1-9][0-9]?)|Y[2-5])-(?:[DO])-(?:[ON]))*)$


[演示链接][1]
英文:

The general rule for capturing a comma-delimited sequence of a pattern is

(pat(?:,pat)*)

where pat is the pattern for a single iteration.

So in your case it would be:

^/live/midrate/v1/rates/((?:[A-Z]{6})-(?:SPT|ON|W[1-3]|M(?:[1-9][0-9]?)|Y[2-5])-(?:[DO])-(?:[ON])(?:,(?:[A-Z]{6})-(?:SPT|ON|W[1-3]|M(?:[1-9][0-9]?)|Y[2-5])-(?:[DO])-(?:[ON]))*)$

DEMO

答案2

得分: 0

我也尝试了这个,并且它有效。

^/live/midrate/v1/rates/((?:([A-Z]{6})-(SPT|ON|TN|W[1-3]|M[1-9][0-9]?|Y[2-5])-([DO])-([ON]),?){1,})$

演示

英文:

I tried this one as well and it works.

^/live/midrate/v1/rates/((?:([A-Z]{6})-(SPT|ON|TN|W[1-3]|M[1-9][0-9]?|Y[2-5])-([DO])-([ON]),?){1,})$

DEMO

huangapple
  • 本文由 发表于 2023年5月25日 23:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定