英文:
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]))*)$
答案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,})$
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论