星空网 > 软件开发 > ASP.net

c# 正则表达式笔记

c# 正则表达式笔记

估计要写几天

看得一个不错的正则教程包括字符串教程 C#字符串和正则表达式参考手册.pdf

 

     正则所需要的命名空间是 using System.Text.RegularExpressions

     它包含了8个类,用得最多是的Regex;

  Regex不仅可以用来创建正则表达式,而且提供了许多有用的方法。

  创建一个Regex对象

  new Regex(string pattern)

     new Regex(string pattern,RegexOptions options)

     第一个参数是个字符串   第二个参数正则配置的选项 有以下一些选项

  • IgnoreCase                 //是匹配忽略大小写      默认情况区分大小写
  • RightToLeft                 //从右到左查找字符串   默认是从左到右
  • None                          //不设定标志  这是默认选项,就是不设置第2个参数  表示区分大小写 从左到右
  • MultiLinc                    //指定了^和$可以匹配行的开头和结尾,也就是说使用了换行分割,每一行能得到不同的匹配
  • SingleLine                 //规定特殊字符"."匹配任一字符,换行符除外. 默认情况下特殊字符"."不匹配换行.(啥意思 都不匹配换行这个参数有啥用  没看懂)

IgnoreCase的例子

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

RightToLeft的例子

   string test = "vvv123===456vvv";
Regex reg = new Regex("\\d+");// 123 从左到右 匹配连续数字
Console.WriteLine(reg.Match(test));
Regex reg1 = new Regex("\\d+",RegexOptions.RightToLeft);
Console.WriteLine(reg1.Match(test));// 456 从右到左 匹配连续数字

MultiLinc的例子

c# 正则表达式笔记
      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);
}
c# 正则表达式笔记

 

IsMatch()

       可以用来测试字符串,看他是否匹配正则表达式的模式.如果发现了一次匹配,就返回True.IsMatch有个静态方法重载

Regex.IsMatch(string str,string pattern);

      string str = "abcbbbbbbbb";
string reg = @"^abc";
Console.WriteLine(Regex.IsMatch(str,reg ));//静态的重载方法
Regex pattern = new Regex("^abc");
Console.WriteLine(pattern.IsMatch(str)); //生成对象上的方法

 

Replace()
        替换字符串一个匹配的模式,也有一个静态的重载方法,replace变体方法很多,我只记录我看到的

replace(string input ,string pattern,int count,int start) 第3个参数是总共替换几个,第4分参数是从字符串的什么位置开始替换

c# 正则表达式笔记
      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)); 静态方法好像不行 会报错 哈哈
c# 正则表达式笔记


Match()

      获得匹配的内容(只是一次的 MatchCollection可以获得所有的的匹配的集合)

      生成的对象上的方法   的用法

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

      第一个参数是要处理的字符串  第二哥参数开始的位置  第3个参数是需要匹配的长度。第2第3个参数可以不需要

      静态方法  Regex.Match(string input , string pattern,RegexOptions options)

      第3个参数可以不要 

c# 正则表达式笔记
      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();
}
c# 正则表达式笔记


Match类的一些方法

  • NextMatch        返回下一个成功匹配的match对象
  • Result
  • Value               返回匹配的字符串
  • Length             匹配的长度
  • Index               第一个匹配内容在字符串中的起始位置
  • Groups             返回一个分组对象集合
  • Success            根据是否匹配成功返回ture or false

 MatchCollection()

            Regex.Matchs会返回MatchCollection类,这个集合包含了所有的Match的集合

c# 正则表达式笔记
      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);
}
c# 正则表达式笔记







原标题:c# 正则表达式笔记

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

温州市跨境电子商务协会:https://www.goluckyvip.com/tag/43443.html
温州综试区:https://www.goluckyvip.com/tag/43446.html
文达科技集团:https://www.goluckyvip.com/tag/43447.html
文达说:https://www.goluckyvip.com/tag/43448.html
文鼎海外仓:https://www.goluckyvip.com/tag/43449.html
热门玩具:https://www.goluckyvip.com/tag/4345.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流