Taha Rehman Siddiqui
Taha Rehman Siddiqui

Reputation: 2533

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

Upvotes: 1

Views: 57

Answers (2)

Tim007
Tim007

Reputation: 2557

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`

Upvotes: 1

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

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) Here is the link for demo. http://regexr.com/3cu60

Upvotes: 1

Related Questions