【问题标题】:find in text file names, preg_match_all在文本文件名中查找 preg_match_all
【发布时间】:2012-06-12 21:13:44
【问题描述】:

我想在文本文件名中查找(例如 name.txt)。 你能建议我在 preg_match_all() 中使用的正则表达式吗? 我创建了一些正则表达式:

preg_match_all("/^[a-zA-Z0-9_.]+[.][a-zA-Z0-9]{1,3}$/", $string, $w);

但是,总是得到一个空的结果。

【问题讨论】:

  • 我认为您应该使用分隔符^' and $`,因为我猜您正在对文本或句子使用preg_match_all

标签: preg-match preg-match-all


【解决方案1】:

只需删除 reg 表达式中的这些符号(^ 和 $)

这对我有用:

preg_match_all("![a-zA-Z0-9_.]+[.][a-zA-Z0-9]{1,3}!", $string, $w);

^ - 匹配字符串的开头。

$ - 匹配字符串的结尾。

如果你的文件放在文本中你可以省略这个符号。

我会使用下面的reg表达式,因为你写的那个找到扩展名由1到3个字母组成的文件,但通常扩展名包含3个字母(.doc,txt)

下面是工作示例:

$string=" some text name.txt  some text hello.doc";
preg_match_all("![a-zA-Z0-9_.]+[.][a-zA-Z0-9]{3}!", $string, $w);
print_r($w);

输出:

Array ( [0] => Array ( [0] => name.txt ) ) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2020-03-03
    • 2015-12-23
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多