日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# 正则表达式笔记

發(fā)布時間:2023/12/10 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# 正则表达式笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

c# 正則表達式筆記

估計要寫幾天

看得一個不錯的正則教程包括字符串教程 C#字符串和正則表達式參考手冊.pdf

?

???? 正則所需要的命名空間是 using System.Text.RegularExpressions

???? 它包含了8個類,用得最多是的Regex;

  Regex不僅可以用來創(chuàng)建正則表達式,而且提供了許多有用的方法。

  創(chuàng)建一個Regex對象

  new Regex(string pattern)

???? new Regex(string pattern,RegexOptions options)

???? 第一個參數(shù)是個字符串?? 第二個參數(shù)正則配置的選項 有以下一些選項

  • IgnoreCase???????????????? //是匹配忽略大小寫?? ?? 默認情況區(qū)分大小寫
  • RightToLeft???????????????? //從右到左查找字符串?? 默認是從左到右
  • None????????????????????????? //不設定標志? 這是默認選項,就是不設置第2個參數(shù)? 表示區(qū)分大小寫 從左到右
  • MultiLinc??????????????????? //指定了^和$可以匹配行的開頭和結尾,也就是說使用了換行分割,每一行能得到不同的匹配
  • SingleLine???????????????? //規(guī)定特殊字符"."匹配任一字符,換行符除外. 默認情況下特殊字符"."不匹配換行.(啥意思 都不匹配換行這個參數(shù)有啥用? 沒看懂)

IgnoreCase的例子

string test = "Abcccccc";
Regex reg = new Regex("abc");
Console.WriteLine(reg.IsMatch(test)); //false
Regex reg1 = new Regex("abc",RegexOptions.IgnoreCase); //不區(qū)分大小寫
Console.WriteLine(reg1.IsMatch(test));//true

RightToLeft的例子

string test = "vvv123===456vvv";
Regex reg = new Regex("\\d+");// 123 從左到右 匹配連續(xù)數(shù)字
Console.WriteLine(reg.Match(test));
Regex reg1 = new Regex("\\d+",RegexOptions.RightToLeft);
Console.WriteLine(reg1.Match(test));// 456 從右到左 匹配連續(xù)數(shù)字

MultiLinc的例子

StringBuilder input = new StringBuilder();
input.AppendLine("A bbbb A");
input.AppendLine("C bbbb C");

string pattern = @"^\w";
Console.WriteLine(input.ToString());
MatchCollection matchCol = Regex.Matches(input.ToString(), pattern, RegexOptions.Multiline);
foreach (Match item in matchCol)
{
Console.WriteLine("結果:{0}", item.Value);
}

?

IsMatch()

?????? 可以用來測試字符串,看他是否匹配正則表達式的模式.如果發(fā)現(xiàn)了一次匹配,就返回True.IsMatch有個靜態(tài)方法重載

Regex.IsMatch(string str,string pattern);

string str = "abcbbbbbbbb";
string reg = @"^abc";
Console.WriteLine(Regex.IsMatch(str,reg ));//靜態(tài)的重載方法
Regex pattern = new Regex("^abc");
Console.WriteLine(pattern.IsMatch(str)); //生成對象上的方法

?

Replace()
??????? 替換字符串一個匹配的模式,也有一個靜態(tài)的重載方法,replace變體方法很多,我只記錄我看到的

replace(string input ,string pattern,int count,int start) 第3個參數(shù)是總共替換幾個,第4分參數(shù)是從字符串的什么位置開始替換

string str = "123456abc";
Regex pattern = new Regex(@"\d+");
Console.WriteLine(pattern.Replace(str,"6666"));
string pattern1 = @"\d+";
Console.WriteLine(Regex.Replace(str,pattern1,"6666"));

string str1 = "asd,sss,asd,asdf,jjjj,cccc";
Regex pattern2 = new Regex(@"\w+");
Console.WriteLine(pattern2.Replace(str1, "v5v5", 2));
Console.WriteLine(pattern2.Replace(str1, "v5v5", 2,8));
// Console.WriteLine(Regex.Replace(str1, @"\w+", "v5v5", 2)); 靜態(tài)方法好像不行 會報錯 哈哈


Match()

????? 獲得匹配的內容(只是一次的 MatchCollection可以獲得所有的的匹配的集合)

????? 生成的對象上的方法?? 的用法

????? reg.Match(string input,int start,int length)

????? 第一個參數(shù)是要處理的字符串? 第二哥參數(shù)開始的位置? 第3個參數(shù)是需要匹配的長度。第2第3個參數(shù)可以不需要

????? 靜態(tài)方法? Regex.Match(string input , string pattern,RegexOptions options)

????? 第3個參數(shù)可以不要?

string str = "vchaha vcoo vclielie vbguale vfgg vckk";
Regex pattern = new Regex(@"vc\w*");
Match matchMode = pattern.Match(str);
while (matchMode.Success)
{
Console.WriteLine(matchMode.Value);
matchMode = matchMode.NextMatch();
}
Console.WriteLine("-----------------------------------");
Match matchMode1 = Regex.Match(str, @"vc\w*");
while (matchMode1.Success)
{
Console.WriteLine(matchMode1.Value);
matchMode1 = matchMode1.NextMatch();
}


Match類的一些方法

  • NextMatch ? ? ?? 返回下一個成功匹配的match對象
  • Result
  • Value?????????????? 返回匹配的字符串
  • Length???????????? 匹配的長度
  • Index?????????????? 第一個匹配內容在字符串中的起始位置
  • Groups???????????? 返回一個分組對象集合
  • Success??????????? 根據(jù)是否匹配成功返回ture or false

?MatchCollection()

??????????? Regex.Matchs會返回MatchCollection類,這個集合包含了所有的Match的集合

string input = "hahaha 123xiaodi 55nihao 66chifanlema ccc333 ccc";
Regex pattern = new Regex(@"\d+[a-z]+",RegexOptions.IgnoreCase);
MatchCollection matchsMade = pattern.Matches(input);
foreach (Match item in matchsMade)
{
Console.WriteLine(item.Value);
}



轉載于:https://www.cnblogs.com/minlecun/archive/2011/12/06/2277399.html

總結

以上是生活随笔為你收集整理的c# 正则表达式笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。