简体   繁体   English

正则表达式:与字符串分开匹配一组文件名

[英]Regex: Match a set of filenames separately from a string

I have googled a lot, got multiple answers none working in my case. 我在Google上搜索了很多,得到了多个答案,但在我的情况下却无济于事。 Sample string this regex should work on is 此正则表达式应使用的示例字符串是

"Purging file summary_t.dat after pre_summary.csv, data.dat clogged"

Regex.Replace("regex", "$fileName") should give this Regex.Replace(“ regex”,“ $ fileName”)应该给这个

"Purging file $fileName after $fileName, $fileName clogged"

The regex's I have tried are 我尝试过的正则表达式是

^[\w,\s-]+\.[A-Za-z]{3}$

It only matches the first filename 它只匹配第一个文件名

(?:[^\\:]+\\)*((?:[^:\\]+)\.\w+)

This one matches everything including text in between multiple filenames, so that I only get one $filename for the whole line 这个匹配所有内容,包括多个文件名之间的文本,因此整行只得到一个$ filename

Note: The string is generic so I dont really know where I can get a filename in a string 注意:字符串是通用的,所以我真的不知道在哪里可以从字符串中获取文件名

Try this 尝试这个

(\w+\.\w+)

https://regex101.com/r/wK3cK7/1 https://regex101.com/r/wK3cK7/1

Output: 输出:

MATCH 1
1.  [13-26] `summary_t.dat`
MATCH 2
1.  [33-48] `pre_summary.csv`
MATCH 3
1.  [50-58] `data.dat`

This must work as well 这也必须工作

\w+\.\w+

\\w = Any letter, + = one or more, \\. = followed by a dot (escape the . meaning, else only . means 0 or more) \\w = Any letter, + = one or more, \\. = followed by a dot (escape the . meaning, else only . means 0 or more) Here is the link for demo. \\w = Any letter, + = one or more, \\. = followed by a dot (escape the . meaning, else only . means 0 or more)这是演示的链接。 http://regexr.com/3cu60 http://regexr.com/3cu60

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM