更新时间:2023-02-26 10:50:06
这是我想到的:
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 上获得.