C#正则表达式与HashTable详解

Janna ·
更新时间:2024-09-20
· 165 次阅读

目录

1、正则表达

匹配规则

转义字符

限定字符

分组()

2、C#中正则表达式构建与匹配

正则表达式的替换

正则表达式拆分

HashTable概述及元素添加

Hashtable遍历

Hashtable元素的删除

HashTable元素查找

任务小结

1、正则表达

Regular Expressiong,Regex、Regexp

常见语法:

字符匹配

重复匹配

字符定位

转义匹配

其他高级语法(字符分组、字符替换和字符决策)

正则表达式软件:RegEx Tester

匹配规则

原样匹配

元字符匹配:

原样匹配的时候,采用的都是不变的直接量字符

采用元字符可以匹配一类字符,元字符可以是一个单独的字符,也可以是一对(前一个字符通常是反斜杠)元字符:

正则表达式 

[a-zA-Z]:匹配a到z的任意字符

[0-9]:匹配0到9的任意数字

转义字符

如果想查找元字符本身,就必须采用\配合对应院子符来取消特定字符。
如果想查找\或.就采用\或.

限定字符

限定字符又叫重复描述字符,标识一个字符要出现的次数

分组()

Ip地址正则表达式:

Ip地址匹配(需验证)
((25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]?\d?\d)

删除空行:^[\s\t]*\n

2、C#中正则表达式构建与匹配

使用方法:

引用命名空间System.Text.RegularExpressions

构造正则表达式

在使用正则表达式时,要先构造正则表达式,这就用到了Regex类。

其构建方式有两种:

基本形式:Regex(正则表达式)

重载形式:Regex(正则表达式,匹配选项)

其中匹配选项提供一些特殊的帮助,是一个枚举值,包括下面六个值:

IgnoreCase(忽略大小写)

ReghtToLeft(从右向左)

None(默认)

CultureInvariant(忽略区域)

MultLine(多行模式)

SingleLine(单行模式)

在Regex类中包含IsMatch(),Replace(),Split()等
IsMatch():测试字符是否满足正则表达式,返回一个布尔值,验证用户输入的数据是否满足条件(例如是否为合法手机号,是否为合法邮箱等)。
IsMatch()的使用格式:Regex.IsMatch(要判断的字符串,正则表达式)

using System; using System.Text.RegularExpressions; namespace 正则表达式的构建及匹配 { class Program { static void Main(string[] args) { //判断是否为北京市固定电话,区号为010,民用电话号码为7-8位 string pattern = @"^(010|010-)\d{7,8}$"; //判定的模式 string[] input = { "010-12345678", "01023415678", "01098765897", "031078965476" }; Console.WriteLine("静态方法"); foreach (string outstr in input) { bool mybool = Regex.IsMatch(outstr.Trim(),pattern); if(mybool) { Console.WriteLine(outstr+"是北京固话"); } else { Console.WriteLine(outstr+"不是北京市固话"); } } //实例化的方式 Console.WriteLine("实例化方式"); foreach (string outstr in input) { Match mymatch = Regex.Match(outstr.Trim(), pattern); if (mymatch.Success) { Console.WriteLine(outstr + "是北京固话"); } else { Console.WriteLine(outstr + "不是北京市固话"); } } Console.ReadKey(); } } } 正则表达式的替换

格式:Regex.Replace(要搜索匹配项的字符串,要替换的原字符串,替换后的字符串);

using System; using System.Text.RegularExpressions; namespace 正则表的是的替换 { class Program { static void Main(string[] args) { //将www.替换为http://www. string mystr = "Welcome to www.darly.net WWW.darly.com WwW.darly.org"; string pattern = @"\bw{3}\.\w+\.(com|net|org)\b"; string replacement1 = @"http://$&"; //$&匹配的内容 string replacement2 = "\n"+ @"http://$&"; //$&匹配的内容,此种方式只是将响应的字符拼接到匹配的字符串前面, Console.WriteLine("替换前的字符串"+mystr); Console.WriteLine("替换后的字符串1-1" + Regex.Replace(mystr, pattern, replacement1)); Console.WriteLine("替换后的字符串2-1" + Regex.Replace(mystr, "www.", @"http://www.")); //此种方式是将匹配的内容替换成目标字符 Console.WriteLine("替换后的字符串1_2" + Regex.Replace(mystr, pattern, replacement2,RegexOptions.IgnoreCase)); Console.WriteLine("替换后的字符串2-2" + Regex.Replace(mystr, "www.", "\n"+@"http://www.",RegexOptions.IgnoreCase)); Regex myregex = new Regex(pattern,RegexOptions.IgnoreCase); string result = myregex.Replace(mystr, replacement2); Console.WriteLine("替换后的字符串3" +result); Regex myregex4 = new Regex(pattern,RegexOptions.IgnoreCase); string result4 = myregex4.Replace(mystr, replacement2); Console.WriteLine("替换后的字符串4" + result4); string pattern5 = @"\bw{3}\."; string replacement5 = "\n" + @"http://www."; Console.WriteLine("替换后的字符串5" + Regex.Replace(mystr,pattern5,replacement5,RegexOptions.IgnoreCase)); Console.ReadLine(); } } } 正则表达式拆分

要通过正则表达式拆分字符串,就要通过Regex类的Split方法,格式为:
Regex.Split(要拆分的字符串,要匹配的正则表达式模式)

using System; using System.Text.RegularExpressions; namespace 正则表达式的拆分 { class Program { static void Main(string[] args) { string input = "一、张三 二、李四 三、王五 四、赵六"; string patern = @"\b[一二三四]、"; Console.WriteLine(Type.GetType((Regex.Split(input, patern)).ToString())); foreach (string outstr in Regex.Split(input,patern)) { if(!string.IsNullOrEmpty(outstr))Console.WriteLine(outstr); } Console.ReadKey(); } } } HashTable概述及元素添加

HashTable也被称作为哈希表,键值对或者关联数组。
用于处理和表写类似Ken/value的减值对,其中Key通常可用来快速查找,同时Key是区分大小写;Value用于存储对应于Key的值。HashTable中key/value键值对均为Object类型,所有HashTable可以支持任何类型的Key/Value键值对。
Hashtable特点:键与值成对存在,键时唯一的不能重复的
HashTable中的每个元素时一个存储在DictionaryEntry对象中的键值对

HashTable优点 :把数据的存储和查找的时间大降低几乎可以看成是常数时间;而代价仅仅小号比较多的内容。然而在当前可利用内存越来越多的情况下,用空间换取时间的做法是可取的。另外,编码比较容易也是他的特点之一。

声明格式:
引入命名空间:using System.Collections;

元素添加方法 Add

using System; using System.Collections; namespace Hasehtable1 { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("name", "darly"); ht.Add("gender", "男"); ht[3] = "王五"; //用此种方式一堆Hashtable去增加元素时应该注意 //如果对应的键key存在只是达到一种重新赋值的结果,如果不存在才会增加对应键值对 ht["gender"] = "女"; //数组通过length可以确定长度 //集合是通过count来确定个数 Console.WriteLine(ht.Count); Console.ReadKey(); } } } Hashtable遍历

遍历用到DictionaryEntry(字典键/值对)

using System; using System.Collections; namespace Hasehtable1 { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("name", "darly"); ht.Add("gender", "男"); ht[3] = "王五"; //用此种方式一堆Hashtable去增加元素时应该注意 //如果对应的键key存在只是达到一种重新赋值的结果,如果不存在才会增加对应键值对 ht["gender"] = "女"; ht["department"] = "测试部"; //数组通过length可以确定长度 //集合是通过count来确定个数 Console.WriteLine(ht.Count); foreach(object myobj in ht) { Console.WriteLine(myobj); //此处仅仅输入的是类型 } foreach (DictionaryEntry myobj in ht) { Console.WriteLine("键为:{0},值为:{1}",myobj.Key,myobj.Value); } foreach(object myobj in ht.Keys) { Console.WriteLine("键为:{0};值为{1}", myobj,ht[myobj]); } Console.ReadKey(); } } } Hashtable元素的删除

Remove

Clear

using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hashtable元素删除 { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add(1, "张飒"); ht.Add(true,"李四"); ht.Add(false, "王五"); ht.Add(3, "赵六"); foreach(DictionaryEntry myde in ht) { Console.WriteLine("键为{0},值为:{1}", myde.Key, myde.Value); } //删除Remove ht.Remove(false); Console.WriteLine("移除结果"); foreach (DictionaryEntry myde in ht) { Console.WriteLine("键为{0},值为:{1}", myde.Key, myde.Value); } //删除Clear,删除所有内容 ht.Clear(); Console.WriteLine("清除结果"); foreach (DictionaryEntry myde in ht) { Console.WriteLine("键为{0},值为:{1}", myde.Key, myde.Value); } Console.ReadLine(); } } } HashTable元素查找

Hashtable特点:键与值成对存在,键时唯一的不能重复的,在查找元素的时候,我们往往是依据键查找值的。
三种方法:(前两种方法是实质是一样的)

Contains

ContainsKey

ContainsValue

using System; using System.Collections; namespace Hashtable元素查找 { 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)) //ContainsKey()存在返回true,不存在返回false Console.WriteLine("存在键=1的元素"); else Console.WriteLine("不存在该该键"); if (ht.ContainsValue("张三")) Console.WriteLine("存在值为张三的元素"); else Console.WriteLine("不存在值为张三的元素"); Console.Read(); } } } 任务小结

统计指定字符串(字符串可自行声明)中汉字的个数以及每个汉字出现的次数,将其输出到屏幕上

编程思路:

判断汉字,可以通过正则表达式

然后可将对应汉字存入ArrayList中

通过一定的运算规则,计算出汉字的个数与出现的次数并将结果对应存入Hashtable

遍历Hashtable即可完成任务实施

using System; using System.Text.RegularExpressions; using System.Collections; namespace 任务小结 { class Program { static void Main(string[] args) { //统计指定字符串(字符串可自行声明)中汉字的个数以及每个汉字出现的次数,将其输出到屏幕上 Console.WriteLine("请输入一个字符串,系统将自动计算汉字个数以及每个汉字的出现次数"); string mystr = Console.ReadLine(); string pattern = @"[^\u4e00-\u9fa5]"; //判定非汉字的字符,将非汉字的字符替换掉就是全部的汉字 Regex myregex = new Regex(pattern); string chnstr = myregex.Replace(mystr, ""); Hashtable ht = new Hashtable(); 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; } else { ht.Add(chnstr[i], val); } } Console.WriteLine("原有的字符串是:" + mystr); foreach (DictionaryEntry de in ht) { Console.WriteLine("汉字{0}出现了{1}次",de.Key,de.Value); } Console.ReadKey(); } } }

到此这篇关于C#正则表达式与HashTable详解的文章就介绍到这了,更多相关C# 正则表达式内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



C# 正则 正则表达式

需要 登录 后方可回复, 如果你还没有账号请 注册新账号