文章目录
初识正则表达式
C#中正则表达式的构建与匹配
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;//引入命名空间
namespace _17._4CSharp中构建正则表达式
{
class Program
{
static void Main(string[] args)
{
string pattern = @"^(0349|0349-)\d{7,8}$";
string[] input = { "010-12345678 ", " 0349-1234567", " 034912345678", "075912345678", "034912345", "15112345678" };
foreach(string outstr in input)
{
静态方法
//bool mybool = Regex.IsMatch(outstr.Trim(), pattern);//Trim移除前导空格和后导空格
//if (mybool)
// Console.WriteLine(outstr + "是朔州的电话");
//else
//{
// Console.WriteLine(outstr + "不是朔州的电话");
//}
//实例化的方法
Match mymatch = Regex.Match(outstr.Trim(),pattern);
if (mymatch.Success) Console.WriteLine(outstr + "是朔州市的电话");
else Console.WriteLine(outstr + "不是朔州市的电话");
}
Console.ReadKey();
}
}
}
结果:
010-12345678 不是朔州市的电话
0349-1234567是朔州市的电话
034912345678是朔州市的电话
075912345678不是朔州市的电话
034912345不是朔州市的电话
15112345678不是朔州市的电话
正则表达式替换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;//引入命名空间
namespace _17._5正则表达式的替换
{
class Program
{
static void Main(string[] args)
{
string input = "Welcome to www.51zxw.net WWW.51zxw.net WwW.51zxw.net";
string pattern = @"\bw{3}\.\w+\.(com|net|org)\b";
string replacement = "\n"+@"http:\\$&";
Console.WriteLine("替换前的字符串:" + input);
Console.WriteLine("替换后的字符串1:" + Regex.Replace(input, pattern, replacement,RegexOptions.IgnoreCase));//区分大小写
Console.WriteLine("替换后的字符串2:" + Regex.Replace(input, "www.", "\n"+@"http:\\www.",RegexOptions.IgnoreCase));
Regex myregex = new Regex(pattern,RegexOptions.IgnoreCase);
string result = myregex.Replace(input, replacement);
Console.WriteLine("替换后的字符串3:" + result);
Console.ReadKey();
}
}
}
结果:
替换前的字符串:Welcome to www.51zxw.net WWW.51zxw.net WwW.51zxw.net
替换后的字符串1:Welcome to
http:\\www.51zxw.net
http:\\WWW.51zxw.net
http:\\WwW.51zxw.net
替换后的字符串2:Welcome to
http:\\www.51zxw.net
http:\\www.51zxw.net
http:\\www.51zxw.net
替换后的字符串3:Welcome to
http:\\www.51zxw.net
http:\\WWW.51zxw.net
http:\\WwW.51zxw.net
正则表达式拆分
Alt+Shift+F10快速添加命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;//引入命名空间
using System.Threading.Tasks;
namespace _17._6正则表达式的拆分
{
class Program
{
static void Main(string[] args)
{
string input = "一、张三 二、李四 三、王五 四、赵六 五、何七";
string pattern = @"\b[一二三四五]、";
foreach (string outstr in Regex.Split(input, pattern))
{
if (!String.IsNullOrEmpty(outstr))//如果输出不是空字符串
Console.WriteLine(outstr);
}
Console.ReadKey();
}
}
}
结果:
张三
李四
王五
赵六
何七
Hashtable概述及元素的添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;//引入命名空间
namespace _17._7HashTable概述及元素的添加
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "张三");
ht.Add(2, "李四");
ht[3] = "王五";//用此种方式对Hashtable去增加元素时应该注意
//如果对应的键key存在,只达到一种重新赋值的结果,若不存在,才会增加对应的键值对
ht[1] = "赵六";
//数组通过length可以确定长度
//集合是通过count来确定个数
Console.WriteLine(ht.Count);
Console.ReadKey();
}
}
}
结果
3
Hashtable元素的遍历
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;//引入命名空间
namespace _17._8Hashtable元素的遍历
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "张三");
ht.Add(2, "李四");
ht.Add(3, "王五");
ht[4] = "赵六";
//for(int i=0;i<ht.Count;i++)
//{
// Console.WriteLine(ht[i+1]);//键的索引是从0开始的,而我们的键值是从1开始的
//}
foreach(DictionaryEntry myobj in ht)
{
Console.WriteLine("键为{0},值为{1}", myobj.Key, myobj.Value);
}
Console.WriteLine("=============================");
foreach(object myobj in ht.Keys)
{
Console.WriteLine("键为{0},值为{1}", myobj, ht[myobj]);
}
Console.ReadKey();
}
}
}
结果:
键为4,值为赵六
键为3,值为王五
键为2,值为李四
键为1,值为张三
=============================
键为4,值为赵六
键为3,值为王五
键为2,值为李四
键为1,值为张三
Hashtable元素的删除
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _17._9Hashtable元素的删除
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "张三");
ht.Add(true, "李四");
ht.Add(false, "王五");
ht.Add(3, "赵六");
ht.Add("abcd","何七");
foreach(DictionaryEntry myde in ht)
{
Console.WriteLine("键为{0},值为{1}", myde.Key, myde.Value);
}
ht.Remove(false);//移除需要加key键
Console.WriteLine("移除的结果为:");
foreach (DictionaryEntry myde in ht)
{
Console.WriteLine("键为{0},值为{1}", myde.Key, myde.Value);
}
ht.Clear();//无参
Console.WriteLine("清除的结果为:");
foreach (DictionaryEntry myde in ht)
{
Console.WriteLine("键为{0},值为{1}", myde.Key, myde.Value);
}
Console.ReadKey();
}
}
}
结果:
键为abcd,值为何七
键为3,值为赵六
键为False,值为王五
键为True,值为李四
键为1,值为张三
移除的结果为:
键为abcd,值为何七
键为3,值为赵六
键为True,值为李四
键为1,值为张三
清除的结果为:
Hashtable元素的查找
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _17._10Hashtable元素的查找
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "张三");
ht.Add(2, "李四");
ht.Add(3, "王五");
ht.Add(4, "赵六");
ht[5] = "何七";
ht[6] = "张三";
Console.WriteLine("添加的结果:");
foreach(DictionaryEntry myde in ht)
{
Console.WriteLine("键为{0},值为{1}", myde.Key, myde.Value);
}
if (ht.ContainsKey(1)) Console.WriteLine("存在键=1");
else Console.WriteLine("不存在该键");
if (ht.ContainsValue("张三")) Console.WriteLine("存在值=张三");
else Console.WriteLine("不存在该值");
Console.ReadKey();
}
}
}
结果:
添加的结果:
键为6,值为张三
键为5,值为何七
键为4,值为赵六
键为3,值为王五
键为2,值为李四
键为1,值为张三
存在键=1
存在值=张三
本章小结及任务实施
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections;
namespace _17._11本章小结及任务实施
{
class Program
{
static void Main(string[] args)
{
string str = "我爱你,我的中国。我喜欢我要自学网";
string pattern = @"[^\u4e00-\u9fa5]";
Regex regex = new Regex(pattern);
string chnstr = regex.Replace(str, "");
Hashtable ht = new Hashtable();
Console.WriteLine(chnstr);
for(int i=0;i<chnstr.Length;i++)
{
int val = 1;
if (ht.ContainsKey(chnstr[i]))
{
val = Convert.ToInt32(ht[chnstr[i]]);
val++;
}
ht[chnstr[i]] = val;
}
foreach(DictionaryEntry de in ht)
{
Console.WriteLine("{0}-----{1}", de.Key, de.Value);
}
Console.ReadKey();
}
}
}
结果:
我爱你我的中国我喜欢我要自学网
国-----1
网-----1
要-----1
自-----1
你-----1
我-----4
中-----1
的-----1
喜-----1
欢-----1
爱-----1
学-----1