Regex实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions; namespace RegexTest
{
class Program
{
static void Main(string[] args)
{
string msg = "Welcome to China come comecome"; var matchs = Regex.Matches(msg, @"\bcome\b");
foreach (var item in matchs)
{
Console.WriteLine(item);
} msg = Regex.Replace(msg,@"\bcome\b","****");
Console.WriteLine(msg); string content = "Welcome to 'China' 'zoro' 'zore' 'zero'"; content = Regex.Replace(content, @"'(\w+)'","[$1]"); Console.WriteLine(content); string str = "我的生日是05/21/2010"; str = Regex.Replace(str, @"(\d+)\/(\d+)\/(\d+)", "$3-$2-$1"); Console.WriteLine(str);
Console.WriteLine("===================="); string str2 = "zxsssssh@itcast.cn"; string s = Regex.Match(str2,@"(.+)@").Groups[].Value;
Console.WriteLine(s);
string left = string.Empty;
for (int i = ; i < s.Length; i++)
{
left += "*";
} string ss = Regex.Replace(str2, @"(.+)@", left+"@");
Console.WriteLine(ss); Console.WriteLine("------------连线去重-----------"); string str3 = "佐佐罗罗";
str3 = Regex.Replace(str3, @"(.)\1+", "$1");
Console.WriteLine(str3); Console.WriteLine("------------查找出XXYY模式的叠词-----------"); string str4 = "浩浩荡荡、清清白白、AABB2、如火如荼、愈演愈烈、AXAY、MNYX,ABCD、没事找事、心服口服、AABBCC";
//查找出XXYY模式的叠词
MatchCollection matchs4 = Regex.Matches(str4, @"(.)\1(.)\2");
foreach (Match item in matchs4)
{
Console.WriteLine(item);
}
Console.WriteLine(matchs4.Count); Console.WriteLine("------------查找出XAXB模式的叠词-----------"); //查找出XAXB模式的叠词
MatchCollection matchs5 = Regex.Matches(str4, @"(.).\1.");
foreach (Match item in matchs5)
{
Console.WriteLine(item);
} Console.WriteLine("------------查找出ABCB模式的叠词-----------"); //查找出XAXB模式的叠词
MatchCollection matchs6 = Regex.Matches(str4, @".(.).\1");
foreach (Match item in matchs6)
{
Console.WriteLine(item);
} Console.WriteLine("------------查找出ABABAB模式的叠词-----------"); //查找出XAXB模式的叠词
MatchCollection matchs7 = Regex.Matches(str4, @"(.)\1(.)\2(.)\3");
foreach (Match item in matchs7)
{
Console.WriteLine(item);
} Console.ReadKey();
}
}
}
上一篇:iOS蓝牙开发(一)蓝牙相关基础知识(转)


下一篇:【python】time和datetime的strptime不是线程安全的!