- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我构建了一个简单的代码来解析几个磁盘中的所有音乐文件夹并将列表放入一个数组中。
当文件夹名称是类别时,文件夹名称以多个空格开头,当它们是“最终文件夹”时,文件夹名称以单个空格开头。例如。看到这个结构:
[0] => /Volumes/SAMPLES/ VOCALS/
[1] => /Volumes/SAMPLES/ VOCALS/ AFRICA/
[2] => /Volumes/SAMPLES/ VOCALS/ AcmeInc Club Vocals/
[3] => /Volumes/SAMPLES/ VOCALS/ AtomicInc Dance Vocals/
[4] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ AfroInc Zulu Vocals/
[5] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/
[6] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/SampleInc_Warriors_Ululation/
[7] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/SampleInc_Warriors_Drums/
等等...我只需要选择最后的文件夹,并尝试了几种贪婪和非贪婪模式的组合,从最后的 $ 开始例如。以下路径不起作用:
$pattern = "#\/ ([:alnum:]+?)/$#i";
$matches = preg_grep ($pattern, $root);
预期的结果应该是:
[3] => /Volumes/SAMPLES/ VOCALS/ AcmeInc Club Vocals/
[4] => /Volumes/SAMPLES/ VOCALS/ AtomicInc Dance Vocals/
[5] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ AfroInc Zulu Vocals/
[6] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/
相反,我得到了所有文件夹或没有文件夹或孤立文件夹。请考虑像 &or 这样的特殊字符!可能在文件夹名称中。感谢您的建议,3天,尝试了一切,绝望,谢谢!
最佳答案
这是一个有效的正则表达式:
'~/(?: +[^/\s]+)*/ [^/\s]+(?: +[^/\s]+)*/$~'
参见 regex demo
它匹配:
/(?: +[^/\s]+)*
- 非最终子文件夹(/
,然后多于1个空格,1个或多个字符除了空格或 /
)/
- 正斜杠后面有一个空格[^/\s]+
- 除了空格或正斜杠之外的 1 个或多个字符(?: +[^/\s]+)*
- 0 个或多个 ...
+
- 1个或多个常规空格[^/\s]+
- 除了空格或正斜杠之外的 1 个或多个字符/
- 正斜杠$
- 字符串结尾参见 PHP code demo :
$ar = array("/Volumes/SAMPLES/ VOCALS/",
"/Volumes/SAMPLES/ VOCALS/ AFRICA/",
"/Volumes/SAMPLES/ VOCALS/ AcmeInc Club Vocals/",
"/Volumes/SAMPLES/ VOCALS/ AtomicInc Dance Vocals/",
"/Volumes/SAMPLES/ VOCALS/ AFRICA/ AfroInc Zulu Vocals/",
"/Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/",
"/Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/SampleInc_Warriors_Ululation/",
"/Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/SampleInc_Warriors_Drums/",
"/Volumes/SAMPLES/ VOCALS/ AFRICA/ AfroInc Zulu Vocals/ Folder1/"
);
$n = preg_grep('~/(?: +[^/\s]+)*/ [^/\s]+(?: +[^/\s]+)*/$~', $ar);
print_r($n);
结果:
Array
(
[2] => /Volumes/SAMPLES/ VOCALS/ AcmeInc Club Vocals/
[3] => /Volumes/SAMPLES/ VOCALS/ AtomicInc Dance Vocals/
[4] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ AfroInc Zulu Vocals/
[5] => /Volumes/SAMPLES/ VOCALS/ AFRICA/ SampleInc Warriors/
)
关于PHP preg_grep 反向匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33288321/
是否有机会使用不区分大小写的搜索在 preg_grep 输入中进行搜索? $checkAuth = preg_grep("/CN=".$cn_name."/", $entries[0]["member
PHP preg_grep 不工作?我是PHP初学者,也可以用英语交流。该程序的执行结果由“ArrayArray”表示... 最佳答案 preg_grep returns匹配行的数组,因此您应该将代
我构建了一个简单的代码来解析几个磁盘中的所有音乐文件夹并将列表放入一个数组中。 当文件夹名称是类别时,文件夹名称以多个空格开头,当它们是“最终文件夹”时,文件夹名称以单个空格开头。例如。看到这个结构:
我有一个由术语组成的数组,其中一些包含重音字符。我像这样做一个 preg grep $data= array('Napoléon','Café'); $result = preg_grep('~' .
我是一名优秀的程序员,十分优秀!