欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#正則表達(dá)式Regex類的常用匹配

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

使用Regex類需要引用命名空間:using System.Text.RegularExpressions;

利用Regex類實(shí)現(xiàn)驗(yàn)證

示例1:注釋的代碼所起的作用是相同的,不過一個(gè)是靜態(tài)方法,一個(gè)是實(shí)例方法

var source = "劉備關(guān)羽張飛孫權(quán)何問起";
//Regex regex = new Regex("孫權(quán)");
//if (regex.IsMatch(source))
//{
// Console.WriteLine("字符串中包含有敏感詞:孫權(quán)!");
//}
if (Regex.IsMatch(source, "孫權(quán)")) 
{
  Console.WriteLine("字符串中包含有敏感詞:孫權(quán)!");
}
Console.ReadLine();

示例2:使用帶兩個(gè)參數(shù)的構(gòu)造函數(shù),第二個(gè)參數(shù)指示忽略大小寫,很常用

var source = "123abc345DEf";
Regex regex = new Regex("def",RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
{
  Console.WriteLine("字符串中包含有敏感詞:def!");
} 
Console.ReadLine();

使用Regex類進(jìn)行替換

示例1:簡單情況

var source = "123abc456ABC789";
// 靜態(tài)方法
//var newSource=Regex.Replace(source,"abc","|",RegexOptions.IgnoreCase);
// 實(shí)例方法
Regex regex = new Regex("abc", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source, "|");
Console.WriteLine("原字符串:"+source);
Console.WriteLine("替換后的字符串:" + newSource);
Console.ReadLine();

結(jié)果:

原字符串:123abc456ABC789

替換后的字符串:123|456|789

示例2:將匹配到的選項(xiàng)替換為html代碼,我們使用了MatchEvaluator委托

var source = "123abc456ABCD789"; 
Regex regex = new Regex("[A-Z]{3}", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source,new MatchEvaluator(OutPutMatch));
Console.WriteLine("原字符串:"+source);
Console.WriteLine("替換后的字符串:" + newSource);
Console.ReadLine();

 //柔城

private static string OutPutMatch(Match match)
{
  return "" +match.Value+ "";
}

輸出:

原字符串:123abc456ABCD789

替換后的字符串:123abc456ABCD789

C#正則表達(dá)式Regex常用匹配

在線測試:http://tool.hovertree.com/a/zz/

#region 身份證號(hào)碼正則表達(dá)式
//何問起

  Console.WriteLine("請輸入一個(gè)身份證號(hào)碼");
  string id = Console.ReadLine();
  bool b4 = Regex.IsMatch(id, @"^\d{15}|\d{18}$");
  bool b5 = Regex.IsMatch(id, @"^(\d{15}|\d{18})$");
  Console.WriteLine(b4);
  Console.WriteLine(b5);

#endregion

#region 匹配電話號(hào)碼
//hovertree


  Console.WriteLine("請輸入電話號(hào)碼");
  string phone = Console.ReadLine();
  bool b = Regex.IsMatch(phone, @"^((\d{3,4}\-\d?{7,8})|(\d{5}))$");
  Console.WriteLine(b);

#endregion

#region 匹配email的regex

//hovertree

  Console.WriteLine("請輸入Email地址");
  string email = Console.ReadLine();
  bool bhvt = Regex.IsMatch(email, @"^\w+@\w+\.\w+$");
  Console.WriteLine(bhvt);

#endregion

#region 匹配ip地址的regex
//hovertree

  Console.WriteLine("請輸入一個(gè)IP地址");
  string ip = Console.ReadLine();
  bool bkly = Regex.IsMatch(ip, @"^\d{1,3}(\.\d{1,3}){3}$");
  Console.WriteLine(bkly);

#endregion

#region 匹配日期合法regex
//何問起

  Console.WriteLine("請輸入一個(gè)日期");
  string date = Console.ReadLine();
  bool bhovertree = Regex.IsMatch(date, @"^\d{4}\-\d{1,2}\-\d{1,2}$");
  Console.WriteLine(bhovertree);

#endregion

#region 匹配url地址的regex
//"http://hovertree.com"
//"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa"
//"https://s.taobao.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8"
//"ftp://127.0.0.1/myslider.txt"

  Console.WriteLine("請輸入url地址");
  string url = Console.ReadLine();
  bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$");
  Console.WriteLine(bkeleyi);

#endregion

上一篇:TortoiseSVN使用教程

欄    目:C#教程

下一篇:C#代碼實(shí)現(xiàn)對AES加密解密

本文標(biāo)題:C#正則表達(dá)式Regex類的常用匹配

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6781.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有