c# 正则表达式
时间:2010-10-20 来源:gull
public static void RunIsMatch()
{
string inputString = "Welcome to the publishers, Wrox Pree Ltd";
if (Regex.IsMatch(inputString, "wrox pree", RegexOptions.IgnoreCase))//匹配在inputString是否存在wrox pree,大小写忽略
{
Console.WriteLine("Match Found");
}
else
{
Console.WriteLine("No Match Found");
}
}
#region Replce() 的用法
public static void RunReplce()
{
string input = "Welcome to the publishers, Wrox Pree Ltd";
input = Regex.Replace(input,"Wrox Pree","wrox pree");
Console.WriteLine(input);
string inputString = "123,456,123,123,789,123,888";
Regex regExp = new Regex("123");
inputString = regExp.Replace(inputString, "xxx", 2, 4);//4表示从第4个开始,2表示为最多替换两次
Console.WriteLine(inputString);
}
#endregion
#region 正则表达式中Split ()的用法
public static void RunSplit()
{
String inputString = "123, ABC,456,DEF,789";
String[] splitResults;
splitResults = Regex.Split(inputString,", |,");//拆分,以逗号或(逗号和空格组合的)
StringBuilder resultBulider = new StringBuilder(32);
Console.WriteLine(splitResults.Length);
foreach (string stringElement in splitResults)
{
resultBulider.AppendFormat("{0}:{1}\n","af",stringElement);
}
Console.WriteLine(resultBulider.ToString());
}
#endregion
/// <summary>
/// 匹配在字符串里都是数字
/// </summary>
/// <param name="regexString">要匹配的字符串的格式</param>
/// <param name="input">要匹配的字符串</param>
/// <param name="beginning">在输入字符串中开始搜索的字符位置。</param>
/// <param name="length">子字符串中包含在搜索中的字符数。</param>
public void RunMatch(string regexString,string input, int beginning, int length)
{
Regex myRegex = new Regex(regexString);
Match matchMade = myRegex.Match(input, beginning, length);
if (matchMade.Length != length)
{
Console.Write("{0}-{1}匹配不成功\n", beginning + 1, length);
}
else
{
Console.WriteLine("{0}-{1}匹配成功\n", beginning + 1, length);
}
#region 在整个字符串中,有其中一些匹配成功就行了,用matchMade.Success
while (matchMade.Success)
{
Console.Write(matchMade.Value);
matchMade = matchMade.NextMatch();
}
#endregion
}
MatchRegular MatchReg = new MatchRegular();
String inPut = "123,456";
String regexString = @"\d+";//匹配0-9的数一个或更多
MatchReg.RunMatch(regexString,inPut, 0, 4);
#region Groups和Capture的用法
public static void RunGroups()
{
String string1 = "04:03:27 127.0.0.1 LibertyAssociates lily";
Regex theReg = new Regex(@"(?<time>(\d|\:)+)\s" + @"(?<ip>(\d|\.)+)\s" + @"(?<site>(\S+)\s)" + @"(?<site>(\S+))");
//我用相同的组site,这里会产生一个问题,后面的组会覆盖前面的组。
MatchCollection theMatches = theReg.Matches(string1);
foreach (Match theMatch in theMatches)
{
if (theMatch.Length != 0)
{
Console.WriteLine("\ntheMatch:{0}", theMatch.ToString());
Console.WriteLine("time:{0}", theMatch.Groups["time"]);
Console.WriteLine("ip:{0}", theMatch.Groups["ip"]);
Console.WriteLine("site:{0}", theMatch.Groups["site"]); //输出lily
Console.WriteLine("site:{0}", theMatch.Groups["site"]); //输出lily
foreach (Capture cap in theMatch.Groups["site"].Captures)//用Capture可以解决这个问题,因为数据其实已经存在了
{
Console.WriteLine("cap:{0}", cap.ToString());
}
}
}
}
#endregion
#region MatchCollection的用法
public static void RunMatchCollection()
{
string s = "this 11is test string";
Regex theReg = new Regex(@"(\d\S+)");\\匹配数字开头后面跟除空格外的字符串
MatchCollection theMatches = theReg.Matches(s);
foreach (Match theMatch in theMatches)
{
Console.WriteLine("theMatch.Length:{0}", theMatch.Length);
if (theMatch.Length != 0)
{
Console.WriteLine("theMatch:{0}",theMatch.ToString());
}
}
}