正则表达式
命名空间:System.Text.RegularExpressions;
常用的类
Regex
Match
MatchCollection
Group
GroupCollection
常用的方法
Regex.IsMatch()
Regex.Match()
Regex.Matches()
Regex.Replace
捕获图片regex=@"<\s?img[^>]+src=""(.+?)""[^>]";
正则表达式组替换例子:
static void Main(string[] args)
{//实现把2012年12月21日换成2012-12-21 或12/21/2012 或2012-12-21
string str = @"2012年12月21日";
在Replace方法中使用"$+数字"表示分组
str = Regex.Replace(str, @"(\d+)年(\d+)月(\d+)日", "$1-$2-$3"); //2012-12-21
// str = Regex.Replace(str, @"(\d+)年(\d+)月(\d+)日", "$2/$3/$1"); //12/21/2012
// str = Regex.Replace(str, @"(\d+)年(\d+)月(\d+)日", "$1year$2month$3day");
-------------------------------------------------麻烦的方法----------------------------------------------------------
Match m = Regex.Match(str, @"(\d+)年(\d+)月(\d+)日");
string temp = string.Format("{0}-{1}-{2}", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
str = Regex.Replace(str, @"\d+年\d+月\d+日", temp);
-------------------------------------------------麻烦的方法----------------------------------------------------------
/*
* 将“我叫张三” -> 我=张三
*
* 我叫张三,他叫李四 -> 我=张三,他=李四
*/
string str = @"我叫张三,他叫李四";
str = Regex.Replace(str, @"(.+?)叫(.+?)", "$1=$2");//取消贪婪模式
Console.WriteLine(str);
Console.ReadKey();
}
例子:
混淆器:
class Program
{
static void Main(string[] args)
{
string content= File.ReadAllText(@"d:\desktop\1.txt",Encoding.Default); //取文件内容
string[] s1 = new string[] { "int", "char", "string" }; //关键字,还可以写更多
List<string> l = new List<string>();
foreach (string st1 in s1) //循环取每个关键字
{
MatchCollection ms = Regex.Matches(content, string.Format(@"{0}\s+(\w+)", st1)); //在文件中取到 关键字后有若干空格再加字符 的整体,并将字符分为组1。
foreach (Match m in ms) //遍历取到的每一个整体
{
if (m.Success)
{
if (!l.Contains(m.Groups[1].Value))
{
l.Add (m.Groups[1].Value); //保存到l中
}
}
}
}
Random r=new Random (); //创建随机对象
foreach (string ls in l) 遍历l中的值,即content中的变量名
{
int tep=r.Next ();
content= Regex.Replace(content,string.Format (@"([^\w]+){0}([^\w]+)",ls), "$1"+(tep % 2 == 0 ? "_" + tep : "o" + tep)+"$2");
}// 变量名左右两边都没有字符, 如果随机偶数则将产生的随机数前面加_替换变量名,产生奇数则在随机数前面加字母o替换变量名
Console.WriteLine(content);
Console.ReadKey();
}
}
某网站职位摘取:
class Program
{
static void Main(string[] args)
{
List<string> l = new List<string>(); //存放不重复的职位信息
string content= File.ReadAllText (@"d:\desktop\1.htm",Encoding.Default); //模拟已经读取的HTML文档
string regex = @"<[^>]+class=""jobname""[^>]+>(.+?)<"; //正则表达式 (从网站上找出规律)
MatchCollection ms=Regex.Matches (content,regex); //取出所有匹配项
foreach (Match m in ms)
{
Console.WriteLine(m.Groups[1].Value); //遍历每一项取出职位对应组的值
if (!l.Contains(m.Groups[1].Value))
{
l.Add(m.Groups[1].Value); //没有重复职位则加到l中。
}
}
Console.WriteLine("\n");
Console.WriteLine("不重复名单:");
Console.WriteLine();
foreach (string s in l)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
不借助第三方变量交换变量的值:
方法1:
num1=num1+num2;
num2=num1-num2;
num1=num1-num2
方法2:
num1=num1|num2;
num2=num1^num2;
num1=num1^num2;
方法3:(仅限c#)
num1=num2+(num2=num1)*0;
委托是一种类型
事件是变量
授权的方法
1.委托变量=方法名;
2.new一个委托对象
委托变量=new 委托类型(方法名);
例子:委托实现
namespace 字符串比较
{ public delegate bool DelegataCompare(string n1,string n2);
class Program
{//字符串排序:
//1.按compare进行比较,排序
//2.按字符串长度进行比较,排序
//3.安其中的数字进行比较,没有数字则默认为0,数字以第一个匹配为主
static void Main(string[] args)
{
while (true)
{
DelegataCompare DP;
string[] str1 = { "aa1", "bb11", "12bv", "few4", "43r", "t43", "t43t34", "t43", "34", "t3t43", "43t43" };
Console.WriteLine("请选择比较模式:1.字符串. 2.字符串长. 3.字符串中数字 ");
string switch1 = Console.ReadLine();
switch (switch1)
{
case "1": DP = Mycompare1; break;
case "2": DP = Mycompare2; break;
case "3": DP = Mycompare3; break;
default: DP = Mycompare1; break;
}
OK(str1, DP);
foreach (string z in str1)
{
Console.WriteLine(z);
}
}
}
static bool Mycompare1(string num1,string num2)
{
return string.Compare(num1, num2) > 0 ? true : false;
}
static bool Mycompare2(string num1,string num2)
{
return num1.Length >num2.Length ?true:false;
}
static bool Mycompare3(string num1, string num2)
{
int n1 = 0;
int n2 = 0;
Match m1 = Regex.Match(num1, @"-?\d+");
Match m2 = Regex.Match(num2, @"-?\d+");
if (m1.Success)
{
n1 = Convert.ToInt32(m1.Value);
}
if (m1.Success)
{
n2 = Convert.ToInt32(m2.Value);
}
return n1 > n2 ? true : false;
}
static void OK(string[] str, DelegataCompare d)
{
for (int i = 0; i < str.Length - 1; i++)
{
for (int j = 0; j < str.Length - 1 - i; j++)
{
if (d(str[j], str[j + 1]))
{
string temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
}
}
多线程的一个例子:开启线程使用System.Threading.Thread类
class Program
{
static void Main(string[] args)
{
// 开启线程使用System.Threading.Thread类
Thread t1 = new Thread(Func1);
Thread t2 = new Thread(Func2);
t1.Start();
t2.Start();
}
static void Func1()
{
for (int i = 0; i < 100000000; i++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(i);
Console.ForegroundColor = ConsoleColor.White;
}
}
static void Func2()
{
for (int i = 0; i < 100000000; i++)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(i);
Console.ForegroundColor = ConsoleColor.White;
}
}
}
命名空间:System.Text.RegularExpressions;
常用的类
Regex
Match
MatchCollection
Group
GroupCollection
常用的方法
Regex.IsMatch()
Regex.Match()
Regex.Matches()
Regex.Replace
捕获图片regex=@"<\s?img[^>]+src=""(.+?)""[^>]";
正则表达式组替换例子:
static void Main(string[] args)
{//实现把2012年12月21日换成2012-12-21 或12/21/2012 或2012-12-21
string str = @"2012年12月21日";
在Replace方法中使用"$+数字"表示分组
str = Regex.Replace(str, @"(\d+)年(\d+)月(\d+)日", "$1-$2-$3"); //2012-12-21
// str = Regex.Replace(str, @"(\d+)年(\d+)月(\d+)日", "$2/$3/$1"); //12/21/2012
// str = Regex.Replace(str, @"(\d+)年(\d+)月(\d+)日", "$1year$2month$3day");
-------------------------------------------------麻烦的方法----------------------------------------------------------
Match m = Regex.Match(str, @"(\d+)年(\d+)月(\d+)日");
string temp = string.Format("{0}-{1}-{2}", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
str = Regex.Replace(str, @"\d+年\d+月\d+日", temp);
-------------------------------------------------麻烦的方法----------------------------------------------------------
/*
* 将“我叫张三” -> 我=张三
*
* 我叫张三,他叫李四 -> 我=张三,他=李四
*/
string str = @"我叫张三,他叫李四";
str = Regex.Replace(str, @"(.+?)叫(.+?)", "$1=$2");//取消贪婪模式
Console.WriteLine(str);
Console.ReadKey();
}
例子:
混淆器:
class Program
{
static void Main(string[] args)
{
string content= File.ReadAllText(@"d:\desktop\1.txt",Encoding.Default); //取文件内容
string[] s1 = new string[] { "int", "char", "string" }; //关键字,还可以写更多
List<string> l = new List<string>();
foreach (string st1 in s1) //循环取每个关键字
{
MatchCollection ms = Regex.Matches(content, string.Format(@"{0}\s+(\w+)", st1)); //在文件中取到 关键字后有若干空格再加字符 的整体,并将字符分为组1。
foreach (Match m in ms) //遍历取到的每一个整体
{
if (m.Success)
{
if (!l.Contains(m.Groups[1].Value))
{
l.Add (m.Groups[1].Value); //保存到l中
}
}
}
}
Random r=new Random (); //创建随机对象
foreach (string ls in l) 遍历l中的值,即content中的变量名
{
int tep=r.Next ();
content= Regex.Replace(content,string.Format (@"([^\w]+){0}([^\w]+)",ls), "$1"+(tep % 2 == 0 ? "_" + tep : "o" + tep)+"$2");
}// 变量名左右两边都没有字符, 如果随机偶数则将产生的随机数前面加_替换变量名,产生奇数则在随机数前面加字母o替换变量名
Console.WriteLine(content);
Console.ReadKey();
}
}
某网站职位摘取:
class Program
{
static void Main(string[] args)
{
List<string> l = new List<string>(); //存放不重复的职位信息
string content= File.ReadAllText (@"d:\desktop\1.htm",Encoding.Default); //模拟已经读取的HTML文档
string regex = @"<[^>]+class=""jobname""[^>]+>(.+?)<"; //正则表达式 (从网站上找出规律)
MatchCollection ms=Regex.Matches (content,regex); //取出所有匹配项
foreach (Match m in ms)
{
Console.WriteLine(m.Groups[1].Value); //遍历每一项取出职位对应组的值
if (!l.Contains(m.Groups[1].Value))
{
l.Add(m.Groups[1].Value); //没有重复职位则加到l中。
}
}
Console.WriteLine("\n");
Console.WriteLine("不重复名单:");
Console.WriteLine();
foreach (string s in l)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
不借助第三方变量交换变量的值:
方法1:
num1=num1+num2;
num2=num1-num2;
num1=num1-num2
方法2:
num1=num1|num2;
num2=num1^num2;
num1=num1^num2;
方法3:(仅限c#)
num1=num2+(num2=num1)*0;
委托是一种类型
事件是变量
授权的方法
1.委托变量=方法名;
2.new一个委托对象
委托变量=new 委托类型(方法名);
例子:委托实现
namespace 字符串比较
{ public delegate bool DelegataCompare(string n1,string n2);
class Program
{//字符串排序:
//1.按compare进行比较,排序
//2.按字符串长度进行比较,排序
//3.安其中的数字进行比较,没有数字则默认为0,数字以第一个匹配为主
static void Main(string[] args)
{
while (true)
{
DelegataCompare DP;
string[] str1 = { "aa1", "bb11", "12bv", "few4", "43r", "t43", "t43t34", "t43", "34", "t3t43", "43t43" };
Console.WriteLine("请选择比较模式:1.字符串. 2.字符串长. 3.字符串中数字 ");
string switch1 = Console.ReadLine();
switch (switch1)
{
case "1": DP = Mycompare1; break;
case "2": DP = Mycompare2; break;
case "3": DP = Mycompare3; break;
default: DP = Mycompare1; break;
}
OK(str1, DP);
foreach (string z in str1)
{
Console.WriteLine(z);
}
}
}
static bool Mycompare1(string num1,string num2)
{
return string.Compare(num1, num2) > 0 ? true : false;
}
static bool Mycompare2(string num1,string num2)
{
return num1.Length >num2.Length ?true:false;
}
static bool Mycompare3(string num1, string num2)
{
int n1 = 0;
int n2 = 0;
Match m1 = Regex.Match(num1, @"-?\d+");
Match m2 = Regex.Match(num2, @"-?\d+");
if (m1.Success)
{
n1 = Convert.ToInt32(m1.Value);
}
if (m1.Success)
{
n2 = Convert.ToInt32(m2.Value);
}
return n1 > n2 ? true : false;
}
static void OK(string[] str, DelegataCompare d)
{
for (int i = 0; i < str.Length - 1; i++)
{
for (int j = 0; j < str.Length - 1 - i; j++)
{
if (d(str[j], str[j + 1]))
{
string temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
}
}
多线程的一个例子:开启线程使用System.Threading.Thread类
class Program
{
static void Main(string[] args)
{
// 开启线程使用System.Threading.Thread类
Thread t1 = new Thread(Func1);
Thread t2 = new Thread(Func2);
t1.Start();
t2.Start();
}
static void Func1()
{
for (int i = 0; i < 100000000; i++)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(i);
Console.ForegroundColor = ConsoleColor.White;
}
}
static void Func2()
{
for (int i = 0; i < 100000000; i++)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(i);
Console.ForegroundColor = ConsoleColor.White;
}
}
}