我希望能够像这样解析文件路径:
/var/www/index.(htm|html|php|shtml)
进入有序数组:
array("htm", "html", "php", "shtml")
然后生成备选方案列表:
/var/www/index.htm
/var/www/index.html
/var/www/index.php
/var/www/index.shtml
现在,我有一个 preg_match
语句可以拆分两个备选方案:
preg_match_all ("/\(([^)]*)\|([^)]*)\)/", $path_resource, $matches);
有人能告诉我如何扩展它以接受无限数量的备选方案(至少两个)吗?只是关于正则表达式,其余的我可以处理。
规则是:
列表需要以
(
开始,以) 结束
列表中必须有一个
|
(即至少有两个选择)(
或)
的任何其他出现都将保持不变。
更新:我还需要能够处理多个括号对,例如:
/var/(www|www2)/index.(htm|html|php|shtml)
抱歉,我没有马上说。
Update 2: If you're looking to do what I'm trying to do in the filesystem, then note that glob() already brings this functionality out of the box. There is no need to implement a custom solutiom. See @Gordon's answer below for details.
最佳答案
我认为您正在寻找:
/(([^|]+)(|([^|]+))+)/
基本上,将分隔符“|”成重复模式。
此外,根据您的第三个要求,您的话应该由“not pipes”而不是“not parens”组成。
此外,对于此问题,更喜欢 +
而不是 *
。 +
表示“至少一个”。 *
表示“零个或多个”。
关于php - 匹配无限数量选项的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2534455/