源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

轻松学习C#的正则表达式

  • 时间:2021-05-23 21:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:轻松学习C#的正则表达式
      在编写处理字符串的程序时,经常会有查找符合某些复杂规则的字符串的需要。正则表达式就是用于描述这些规则的工具。正则表达式拥有一套自己的语法规则,常见语法包括字符匹配,重复匹配,字符定位,转义匹配和其他高级语法(字符分组,字符替换和字符决策),使用正则表达式时,首先构造正则表达式,这就用到了Regex类。其构造方式有两种:         [b]基本形式Regex(正则表达式)         基本形式Regex(正则表达式,匹配选项) [/b]其中匹配选项是提供一些特殊的帮助,是一个枚举值,包括下面六个值: [list] [*]        (1)IgnoreCase(忽略大小写)[/*] [*]        (2)ReghtToLeft(从左向右)[/*] [*]        (3)None(默认)[/*] [*]        (4)CultureInvariant(忽略区域)[/*] [*]        (5)Multline(多行模式)[/*] [*]        (6)SingleLine(单行模式)[/*] [/list] [b]一、正则表达式的匹配[/b]         正则表达式的匹配是通过Regex类的IsMatch方法实现的,IsMatch方法的使用格式为:         [b]Regex.IsMatch(要判断的字符串,正则表达式) [/b][b]例一:利用IsMatch方法判断是否符合当地的电话号码的程序[/b]
<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions;//引入命名空间 
using System.Threading.Tasks; 
 
namespace 正则表达式 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 string str = @"(0530|0530-)\d{7,8}";//定义的正则表达式符合条件为“0530”或 
 Console.WriteLine("请输入一个电话号码");//“0530-”开头,后面跟7位或8位数字 
 string tel = Console.ReadLine(); 
 bool b; 
 b = Regex.IsMatch(tel,str);//判断是否符合正则表达式 
 if (b) 
 { 
 Console.WriteLine("{0}是某地的电话号码",tel); 
 } 
 else 
 { 
 Console.WriteLine("{0}不是某地的电话号码",tel); 
 } 
 Console.ReadLine(); 
 } 
 } 
}</span> 
输入:0530-12345678 输出的结果为:0530-12345678是某地的电话号码 [b]二、正则表达式的替换 [/b]        要通过正则表达式替换字符串中的匹配项,就要通过Regex类中的Replace方法。通常用的一种格式为:         Regex.Replace(要搜索匹配项的字符串,要替换的原字符串,替换后的字符串) [b]例二:利用Replace方法将用户输入的电子邮件中的“@”替换为“AT”的程序 [/b]
<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
 
namespace ConsoleApplication2 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 string str = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";//定义的电子邮件地址的正则表达式 
 Console.WriteLine("请输入一个正确的Internet电子邮件地址"); 
 string email = Console.ReadLine(); 
 bool b; 
 b = Regex.IsMatch(email, str);//判断是否符合正则表达式 
 if (b) 
 { 
 string outstr = ""; 
 outstr = Regex.Replace(email, "@", "AT");//进行替换 
 Console.WriteLine("替换后为:{0}", outstr); 
 } 
 else 
 { 
 Console.WriteLine("你所输入的字符串中不包括Internet URL"); 
 } 
 Console.ReadLine(); 
 } 
 } 
}</span> 
输入:123456@126.com 输出的结果为:123456AT126.com [b]三、正则表达式的拆分[/b]            要通过正则表达式拆分字符串,就要通过Regex类的Split方法,格式为:            Regex.Split(要拆分的字符串,要匹配的正则表达式模式) [b]例三:通过Split方法进行输入的字符串的拆分 [/b]
<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 
 
namespace ConsoleApplication2 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 string str = ";";//定义的正则表达式 
 Console.WriteLine("请输入多个用户姓名,以分号隔开"); 
 string names = Console.ReadLine(); 
 string[] name; 
 name = Regex.Split(names,str); 
 Console.WriteLine("分隔后的姓名为:"); 
 foreach (string item in name) 
 { 
 Console.WriteLine(item); 
 } 
 Console.ReadLine(); 
 } 
 } 
}</span> 
输入:张三;李四;王五 输出的结果为:张三                        李四                        王五 以上就是C#的正则表达式,希望对大家的学习有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部