正则表达式根据文件扩展名列表获取文件名

时间:2015-08-20 17:42:41

标签: regex

我有以下正则表达式,它似乎可以识别提供的列表中的字符串,但是我需要从识别的字符串到令牌的开头抓取所有字符,不管字符是什么。

^.*?\b(png|dll|exe|docx|pdf)\b.*$

我正在搜索以下测试字符串:

The filename is test.exe will I be able to find it
The file name is bob.png
the file name is my filename.pdf, we love pdfs
the file name is question.png
this is my word file.docx
this is my odd file[.]png

我想找到:

test.exe
bob.png
filename.pdf
question.png
file.docx
file[.]png

我也意识到......可能存在"我们喜欢pdfs"这种方法也是如此..所以任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

您要求的内容:

只需查看导致扩展名的大量非空格字符:

id

演示:https://regex101.com/r/aJ3gV5/1

以上在python中工作,来说明差异。注意底线上只有一个匹配。

nso1_students

只是为了说明同一输入下面的正则表达式:

id

我的建议:

匹配完整的行是没有意义的,除非你只想要每行的第一个匹配。你可以用这个找到更多的结果:

nso1_students

演示:https://regex101.com/r/aJ3gV5/2

既然你在另一个评论中说过你可能会使用python,这里是在python中工作的:

psuid

编辑:

强制点的匹配也可以通过增加正则表达式的开头来完成:

lname

演示:https://regex101.com/r/aJ3gV5/4