2010-11-24 62 views
-1

我想在文本文件的内容中找到特定的模式。任何人都可以帮助我编码如何使用regularexpression(带语法)在c#.net中实现这一点?如何在c#.net中使用RegularExpression?

+2

你将不得不做多一点点比解释。你有没有任何IDE的正则表达式的深度? – 2010-11-24 06:21:02

+0

内容的样本将需要有人在这里为你制定一个正则表达式。你也应该用正则表达式来标记这个问题。有关如何在C#中使用该正则表达式的帮助,请查看http://msdn.microsoft.com/zh-cn/library/ms228595(v=VS.100).aspx – basarat 2010-11-24 06:37:41

回答

0

这基本上取决于你想识别的模式。正如其他人所说,你应该对你的模式期望更具体。

首先,您需要了解正则表达式语法(通常是POSIX,历史上是UNIX,但它是跨语言/平台语法)的基本知识:请看this reference site

然后去你喜欢的C#编辑器,键入:

using System.Text.RegularExpressions; 

StreamReader sr = new StreamReader(yourtextfilepath); 
string input; 
string pattern = @"\b(\w+)\s\1\b";//Whatever Regular Expression Pattern goes here 
while (sr.Peek() >= 0) 
{ 
    input = sr.ReadLine(); 
    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); 
    MatchCollection matches = rgx.Matches(input); 
    if (matches.Count > 0) 
    {   
     foreach (Match match in matches) 
     //Print it or whatever 
     Console.WriteLine(match.Value); 
    } 
} 
sr.Close();