Good morning guys
Is there a good way to use regular expression in C# in order to find all filenames and their paths within a string variable?
For example, if you have this string:
string s = @"Hello John
these are the files you have to send us today: <file>C:\Development\Projects 2010\Accounting\file20101130.csv</file>, <file>C:\Development\Projects 2010\Accounting\orders20101130.docx</file>
also we would like you to send <file>C:\Development\Projects 2010\Accounting\customersupdated.xls</file>
thank you";
The result would be:
C:\Development\Projects 2010\Accounting\file20101130.csv
C:\Development\Projects 2010\Accounting\orders20101130.docx
C:\Development\Projects 2010\Accounting\customersupdated.xls
EDITED:
Considering what told @Jim, I edited the string adding tags in order to make it easier to extract needed file names from string!
Here’s something I came up with:
Produces: (see on ideone)
The regex is not extremely robust (it does make a few assumptions) but it worked for your examples as well.
Here is a version of the program if you use
<file>tags. Change the regex andExtractto:Also available on ideone.