日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php 匹配多个正则表达式,php – 正则表达式匹配无限数量的选项

發(fā)布時間:2024/1/8 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 匹配多个正则表达式,php – 正则表达式匹配无限数量的选项 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

搜索熱詞

我希望能夠像這樣解析文件路徑:

/var/www/index.(htm|html|PHP|shtml)

進入有序數(shù)組:

array("htm","html","PHP","shtml")

然后生成一個備選列表:

/var/www/index.htm

/var/www/index.html

/var/www/index.PHP

/var/www/index.shtml

現(xiàn)在,我有一個preg_match語句可以拆分兩個選項:

preg_match_all ("/\(([^)]*)\|([^)]*)\)/",$path_resource,$matches);

有人可以給我一個指針,如何擴展它以接受無限數(shù)量的替代品(至少兩個)?關(guān)于正則表達式,其余的我可以處理.

規(guī)則是:

>列表需要以a開頭(并以a結(jié)尾)

>必須有一個|在清單中(即至少兩個備選方案)

>(或)的任何其他事件將保持不變.

更新:我需要能夠處理多個括號對,例如:

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

非正則表達式解決方案:)

$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,$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)'));

總結(jié)

如果覺得編程之家網(wǎng)站內(nèi)容還不錯,歡迎將編程之家網(wǎng)站推薦給程序員好友。

本圖文內(nèi)容來源于網(wǎng)友網(wǎng)絡(luò)收集整理提供,作為學(xué)習(xí)參考使用,版權(quán)屬于原作者。

總結(jié)

以上是生活随笔為你收集整理的php 匹配多个正则表达式,php – 正则表达式匹配无限数量的选项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。