开发者

How to use RegularExpression in c# .net?

开发者 https://www.devze.com 2023-01-26 09:51 出处:网络
I want to find a particular pattern in the content of textfile. can anybody help me with codi开发者_StackOverflow中文版ng how to implement this in c# .net using regularexpression(with syntax)?Google i

I want to find a particular pattern in the content of textfile. can anybody help me with codi开发者_StackOverflow中文版ng how to implement this in c# .net using regularexpression(with syntax)?


Google is your friend, give it a try. http://www.google.com.pk/#sclient=psy&hl=en&q=regular+expression+c%23+tutorial&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&psj=1&fp=9045c49e6667deb3


This basically depends on the pattern you want to identify. As others stated, you should be more specific about your pattern expectations.

First off, you need to get a basic knowledge on Regular Expression Syntax (usually POSIX, historically UNIX, but it is a cross-language/platform syntax) : Take a look at this reference site.

Then go to your favorite c# editor and type this :

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消