匹配不限数量的选项的正则表达式

本教程将介绍匹配不限数量的选项的正则表达式的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

匹配不限数量的选项的正则表达式 教程 第1张

问题描述

我希望能够分析如下文件路径:

 /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)

抱歉,我没有直接说出来。

更新2:如果您希望在文件系统中执行我正在尝试执行的操作,请注意,GLOB()已经提供了该功能。不需要实施定制解决方案。有关详细信息,请参阅下面@Gordon的答案。

推荐答案

非正则表达式解决方案:)

<?php

$test = '/var/www/index.(htm|html|php|shtml)';

/**
 *
 * @param string $str "/var/www/index.(htm|html|php|shtml)"
 * @return array "/var/www/index.htm", "/var/www/index.php", etc
 */
function expand_bracket_pair($str)
{
 // Only get the very last "(" and ignore all others.
 $bracketStartPos = strrpos($str, '(');
 $bracketEndPos = strrpos($str, ')');

 // Split on ",".
 $exts = substr($str, $bracketStartPos, $bracketEndPos - $bracketStartPos);
 $exts = trim($exts, '()|');
 $exts = explode('|', $exts);

 // List all possible file names.
 $names = array();

 $prefix = substr($str, 0, $bracketStartPos);
 $affix = substr($str, $bracketEndPos + 1);
 foreach ($exts as $ext)
 {
  $names[] = "{$prefix}{$ext}{$affix}";
 }

 return $names;
}

function expand_filenames($input)
{
 $nbBrackets = substr_count($input, '(');

 // Start with the last pair.
 $sets = expand_bracket_pair($input);

 // Now work backwards and recurse for each generated filename set.
 for ($i = 0; $i < $nbBrackets; $i++)
 {
  foreach ($sets as $k => $set)
  {
$sets = array_merge(
 $sets,
 expand_bracket_pair($set)
);
  }
 }

 // Clean up.
 foreach ($sets as $k => $set)
 {
  if (false !== strpos($set, '('))
  {
unset($sets[$k]);
  }
 }
 $sets = array_unique($sets);
 sort($sets);

 return $sets;
}

var_dump(expand_filenames('/(a|b)/var/(www|www2)/index.(htm|html|php|shtml)'));

好了关于匹配不限数量的选项的正则表达式的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。