更新时间:2022-10-18 17:44:54
没有短途方法可以做到这一点。但是,您可以定义期望的最大项目数,并为每个项目创建一个可选组。
对于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]
*感谢 @sln 提出改进建议
输出网址
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]
*Thanks to @sln for suggesting an improvement
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]