English 中文(简体)
如何从文本中提取. txt 的文件名?
原标题:how to extract file names with .txt from a text?
  • 时间:2012-05-23 18:31:00
  •  标签:
  • linq
  • c#-4.0

我有一个像下面一样的文字,

Lorem ipsum dolor 坐在座椅上, concomectetur < strong> sample1.txt adispisclit elit. Morbi nec urna not ante varius semper emper lippet lipsum ipsum. Pellentesque 常住者 < engy> sample2.t orbi tristique senectus et et menuda fames. 和 menectus 和 menuda 名声。

我在上述文本中有样本1. txt和样本2. txt。 名称与样本1和样本2不同。 我只需要使用 c# 来获取文件名 。

有人能帮我吗?

最佳回答

既然你贴了LINQ标签:

var filesnames = text.Split(new char[] { }) // split on whitespace into words
                     .Where(word => word.EndsWith(".txt"));
问题回答

尝尝这个吧

var filesnames = text.Split(   )
                 .Where(o => o.EndsWith(".txt")).Select(o => o.SubString(o.LastIndexOf( . ))).ToList();

如果有一个很好的方法可以捕捉到您的文件名称的外观, 则可能使用正则表达式。 我假设这里总是有字母数字字符 < code> blah. txt :

var matches = Regex.Matches(input, @"[a-zA-Z0-9]+.txt");




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签