使用C#正则表达式匹配相关字符串

开发 后端
利用C#正则表达式匹配相关字符串,一般使用System.Text.RegularExpressions方法命名空间或使用Matches()方法匹配字符串。

C#正则表达式匹配字符串的方法如下:

1.使用C#中使用正则表达式System.Text.RegularExpressions命名空间;

2.使用C#中使用正则表达式Matches()方法匹配字符串,格式如下:
MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

其中Str表示输入字符串,Pattern表示匹配模式,RegexOptions.IgnoreCase表示忽略大小写,RegexOptions.ExplicitCapture表示改变收集匹配方式。

3.匹配结果保存在MatchCollection集合中,可以通过循环遍历结果集取出Match对象的结果。

TestRagular.cs:

using System;  
using System.Text.RegularExpressions;  
 
namespace Magci.Test.Strings  
{  
    public class TestRegular  
    {  
        public static void WriteMatches(string str, MatchCollection matches)  
        {  
            Console.WriteLine("\nString is : " + str);  
            Console.WriteLine("No. of matches : " + matches.Count);  
            foreach (Match nextMatch in matches)  
            {  
                //取出匹配字符串和最多10个外围字符  
                int Index = nextMatch.Index;  
                string result = nextMatch.ToString();  
                int charsBefore = (Index < 5) ? Index : 5;  
                int fromEnd = str.Length - Index - result.Length;  
                int charsAfter = (fromEnd < 5) ? fromEnd : 5;  
                int charsToDisplay = charsBefore + result.Length + charsAfter;  
 
                Console.WriteLine("Index: {0},\tString: {1},\t{2}", Index, result, str.Substring(Index - charsBefore, charsToDisplay));  
            }  
        }  
 
        public static void Main()  
        {  
            string Str = @"My name is Magci, for short mgc. I like c sharp!";  
 
            //查找“gc”  
            string Pattern = "gc";  
            MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);  
 
            WriteMatches(Str, Matches);  
              
            //查找以“m”开头,“c”结尾的单词  
            Pattern = @"\bm\S*c\b";  
            Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);  
 
            WriteMatches(Str, Matches);  
        }  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.

【编辑推荐】

  1. 介绍Mono C#编译器
  2. C#运算符重载学习总结
  3. 概述C#语言的结构体
  4. C#遗传算法学习笔记
  5. 讨论C#分部方法
责任编辑:彭凡 来源: mgc.ahau.edu.cn
相关推荐

2024-09-30 11:16:39

C#正则表达式

2009-08-20 14:31:55

C#正则表达式字符串

2009-09-16 17:02:15

正则表达式匹配字符串

2009-08-20 13:38:58

C#正则表达式

2009-09-16 17:54:31

正则表达式实现

2009-08-17 13:56:28

C#正则表达式入门

2009-08-07 15:16:10

C#正则表达式

2009-08-03 17:27:14

C#正则表达式

2009-08-20 16:13:32

C#正则表达式匹配

2009-08-20 13:26:35

C#正则表达式

2009-08-17 13:49:23

C#正则表达式提取

2009-08-14 16:50:59

C#正则表达式语法

2009-08-11 13:00:41

C#正则表达式

2009-08-13 15:24:27

C#正则表达式

2024-12-16 07:33:45

C#正则表达式

2010-03-15 16:21:28

Python正则表达式

2009-08-20 13:30:38

C#正则表达式

2021-03-02 07:33:13

开发C#字符

2009-08-13 15:02:52

C#正则表达式引擎贪婪

2009-08-20 14:57:00

C#正则表达式
点赞
收藏

51CTO技术栈公众号