Jun*_*r M 6 c# regex filenames
早上好家伙
有没有一种在C#中使用正则表达式的好方法,以便在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";
Run Code Online (Sandbox Code Playgroud)
结果将是:
C:\Development\Projects 2010\Accounting\file20101130.csv
C:\Development\Projects 2010\Accounting\orders20101130.docx
C:\Development\Projects 2010\Accounting\customersupdated.xls
Run Code Online (Sandbox Code Playgroud)
编辑: 考虑到@Jim的内容,我编辑了字符串添加标签,以便更容易从字符串中提取所需的文件名!
这是我想出来的:
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
string s = @"Hello John these are the files you have to send us today:
C:\projects\orders20101130.docx also we would like you to send
C:\some\file.txt, C:\someother.file and d:\some file\with spaces.ext
Thank you";
Extract(s);
}
private static readonly Regex rx = new Regex
(@"[a-z]:\\(?:[^\\:]+\\)*((?:[^:\\]+)\.\w+)", RegexOptions.IgnoreCase);
static void Extract(string text)
{
MatchCollection matches = rx.Matches(text);
foreach (Match match in matches)
{
Console.WriteLine("'{0}'", match.Value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
产生:(见ideone)
'C:\projects\orders20101130.docx', file: 'orders20101130.docx'
'C:\some\file.txt', file: 'file.txt'
'C:\someother.file', file: 'someother.file'
'd:\some file\with spaces.ext', file: 'with spaces.ext'
Run Code Online (Sandbox Code Playgroud)
正则表达式不是非常强大(它确实做了一些假设)但它也适用于您的示例.
如果您使用<file>标签,这是该程序的一个版本.更改正则表达式并Extract:
private static readonly Regex rx = new Regex
(@"<file>(.+?)</file>", RegexOptions.IgnoreCase);
static void Extract(string text)
{
MatchCollection matches = rx.Matches(text);
foreach (Match match in matches)
{
Console.WriteLine("'{0}'", match.Groups[1]);
}
}
Run Code Online (Sandbox Code Playgroud)