简体   繁体   中英

Regex: Match a set of filenames separately from a string

I have googled a lot, got multiple answers none working in my case. 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

"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

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

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. http://regexr.com/3cu60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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