php regex - 干净的文件名

标签 php regex preg-replace

我正在寻找优雅的正则表达式来清理括号,内容看起来像文件名。

[Nibh justo] elit Nulla [link.pdf]  auctor ipsum molestie (link.pdf) 
Condimentum euismod non [link.xls](link.xls) [link.doc](link.doc) tempus 
In [Curabitur] et

结果应该是:

Nibh justo elit Nulla auctor ipsum molestie Condimentum euismod 
non tempus In Curabitur et

我相信一定有一个捷径可以做到这一点。 (文件的意思很简单 - 包括点。不需要句子检查。)

感谢帮助

最佳答案

是这样的吗?

$str = '[Nibh justo] elit Nulla [link.pdf]  auctor ipsum molestie (link.pdf) 
Condimentum euismod non [link.xls](link.xls) [link.doc](link.doc) tempus In 
[Curabitur] and other [./beta/link.pfd]';

$str = preg_replace('`(\(|\[)[\w/\.-]+\.[a-z]+(\)|\])`i', '', $str);
$str = str_replace(array('[', ']'), '', $str);

echo $str;

结果是:

Nibh justo elit Nulla auctor ipsum molestie Condimentum euismod 
non tempus In Curabitur and other

关于php regex - 干净的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10396396/

相关文章:

如果条目是数字,PHP 仅删除行

php - preg_replace_callback 在参数 2 处调用 $this->method ('\\1' )

php - 使用正则表达式获取标签内的引​​号

java - 我应该使用什么正则表达式来检查 Java 中的字符串是否只有数字和 2 个特殊字符( - 和 , )?

python-3.x - 仅当引号包含 3 个或更多字符时才拆分

php - 如何将cid链接转换为http链接?

javascript - 从 HTML5 网络摄像头获取图像的 src

php - 被显示图像卡住了

php - 我可以在 Swift 的 header 中使用 token 来授权数据库连接吗?

regex - VB.Net正则表达式匹配