使用RegEx在字符串中查找一组文件名路径的方法 [英] A way to use RegEx to find a set of filenames paths in a string
问题描述
早上好!
是否存在在C#中使用正则表达式的好方法,以便在string
变量中查找所有文件名及其路径?
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";
结果将是:
C:\Development\Projects 2010\Accounting\file20101130.csv
C:\Development\Projects 2010\Accounting\orders20101130.docx
C:\Development\Projects 2010\Accounting\customersupdated.xls
已编辑: 考虑到@Jim的内容,我编辑了添加字符串的标签,以便更轻松地从字符串中提取所需的文件名!
EDITED: Considering what told @Jim, I edited the string adding tags in order to make it easier to extract needed file names from string!
推荐答案
这是我想到的:
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);
}
}
}
产生:(请参见 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'
regex并不是非常健壮(它确实做了一些假设),但它也适用于您的示例.
The regex is not extremely robust (it does make a few assumptions) but it worked for your examples as well.
如果使用<file>
标记,则为程序的版本.将正则表达式和Extract
更改为:
Here is a version of the program if you use <file>
tags. Change the regex and Extract
to:
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]);
}
}
也可以在 ideone 上获得.
这篇关于使用RegEx在字符串中查找一组文件名路径的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!