重复正则表达式捕获组的捕获部分

重复正则表达式捕获组的捕获部分

本文介绍了重复正则表达式捕获组的捕获部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ^([az-] +-on-sale(?:,, [az-] +-on-sale){0,})[\ /] $ 

此正则表达式在htaccess文件中使用,并且匹配这样的模式:

 销售工具,销售糖果,销售食品/ 
  ^(([[az-] +)-on -sale(?:,[az-] +-on-sale){0,})[\ /] $ 

这样,我将工具隔离在其自己的捕获组中,但第二部分似乎无法做到这一点。

解决方案

没有短途方法可以做到这一点。但是,您可以定义期望的最大项目数,并为每个项目创建一个可选组。



对于1至3个项目:

  ^([az-] +)-on-sale(?:(,(az-] +)-on- sale(?:(,(az-] +)on-sale)?)?/ $ 






请求网址

  http://foo.bar/tools-on-sale,糖果出售,食品出售/ 

htaccess

  RewriteRule ^([ az-] +)-on-sale(?:(,[az-] +)-on-sale(?:(,[az-] +)-on-sale)?)?/ $ http:// foo .bar / $ 1 $ 2 $ 3 [L] 


输出网址

  http://foo.bar/tools,candy,fo od 






但是,如果您需要的分隔符不是逗号,如果您的商品少于3个,则会生成空令牌。例如:

  http://foo.bar/tools-- 

如果必须避免,则需要为每个项目数创建1条规则:

  RewriteRule ^([[az-] +)待售,([az-] +)待售,([ az-] +)-on-sale / $ http://foo.bar/$1-$2-$3 [L] 
RewriteRule ^([az-] +)-on-sale,([[az-] +)-on-sale / $ http://foo.bar/$1-$2 [L]
RewriteRule ^([az-] +)-on-sale / $ http://foo.bar/$1 [L]


^([a-z-]+-on-sale(?:,[a-z-]+-on-sale){0,})[\/]$

This regex is used in a htaccess file and matches a pattern such as this one:

tools-on-sale,candy-on-sale,food-on-sale/

I've been wondering whether it's possible or not for me to capture a subsection of a repeated capture group. I want to match the same pattern, but I want to omit the "-on-sale" part in the repeated capture group. I know I can already do this for the first part of the regex:

^(([a-z-]+)-on-sale(?:,[a-z-]+-on-sale){0,})[\/]$

That way I have "tools" isolated in its own capture group, but I can't seem to do with the same with the second part. Is this even doable with a regex?

解决方案

There is not a short way to achieve this. However, you could define a maximimum number of items you should expect, and create one optional group for each.

For 1 to 3 items:

^([a-z-]+)-on-sale(?:(,[a-z-]+)-on-sale(?:(,[a-z-]+)-on-sale)?)?/$


Request url

http://foo.bar/tools-on-sale,candy-on-sale,food-on-sale/

htaccess

RewriteRule ^([a-z-]+)-on-sale(?:(,[a-z-]+)-on-sale(?:(,[a-z-]+)-on-sale)?)?/$ http://foo.bar/$1$2$3 [L]

Output url

http://foo.bar/tools,candy,food


However, if you need a delimiter other than commas, this will generate empty tokens if you have less than 3 items. E.g:

http://foo.bar/tools--

If you must avoid it, you need to create 1 rule for each number of items:

RewriteRule ^([a-z-]+)-on-sale,([a-z-]+)-on-sale,([a-z-]+)-on-sale/$ http://foo.bar/$1-$2-$3 [L]
RewriteRule ^([a-z-]+)-on-sale,([a-z-]+)-on-sale/$ http://foo.bar/$1-$2 [L]
RewriteRule ^([a-z-]+)-on-sale/$ http://foo.bar/$1 [L]

这篇关于重复正则表达式捕获组的捕获部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:58