微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式

正则表达式是一种可以和输入文本相匹配的表达式。.Net framework 提供了一个正则表达式引擎让这种匹配成为可能。一个表达式可以由一个或多个字符,运算符,或结构体组成。

构建正则表达式的定义

有很多种类的字符,运算符,结构体可以定义正则表达式。

  • 转义字符
  • 字符类
  • 集合
  • 分组构造
  • 限定符
  • 回溯引用构造
  • 可替换结构
  • 替换
  • 混合结构

Regex 正则表达式类

Regex 类用于表示一个正则表达式。它有下列常用的方法

Sr.No 方法
1 public bool IsMatch(string input)
指出输入的字符串中是否含有特定的正则表达式。
2 public bool IsMatch(string input,int startat)
指出输入的字符串中是否含有特定的正则表达式,该函数的 startat 变量指出了字符串开始查找的位置。
3 public static bool IsMatch(string input,string pattern)
指出特定的表达式是否和输入的字符串匹配。
4 public MatchCollection Matches(string input)
在所有出现的正则表达式中搜索特定的输入字符
5 public string Replace(string input,string replacement)
一个特定的输入字符中,用特定的字符串替换所有满足某个表达式的字符串。
6 public string[] Split(string input)
一个输入字符拆分成一组子字符串,从一个由正则表达式指出的位置上开始。

有关属性方法的完成列表,请参见微软的 C# 文档。

示例 1

下列的例子中找出了以 s 开头的单词:

using System;
    using System.Text.RegularExpressions;

    namespace RegExApplication
    {
        class Program
        {
            private static void showMatch(string text,string expr)
            {
                Console.WriteLine("The Expression: " + expr);
                MatchCollection mc = Regex.Matches(text,expr);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m);
                }
            }

            static void Main(string[] args)
            {
                string str = "A Thousand Splendid Suns";

                Console.WriteLine("Matching words that start with 'S': ");
                showMatch(str,@"\bS\S*");
                Console.ReadKey();
            }
        }
    }

编译执行上述代码,得到如下结果:

Matching words that start with 'S':
    The Expression: \bS\S*
    Splendid
    Suns

示例 2

下面的例子中找出了以 m 开头以 e 结尾的单词

using System;
    using System.Text.RegularExpressions;

    namespace RegExApplication
    {
        class Program
        {
            private static void showMatch(string text,expr);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m);
                }
            }
            static void Main(string[] args)
            {
                string str = "make maze and manage to measure it";

                Console.WriteLine("Matching words start with 'm' and ends with 'e':");
                showMatch(str,@"\bm\S*e\b");
                Console.ReadKey();
            }
        }
    }

编译执行上述代码,得到如下结果:

Matching words start with 'm' and ends with 'e':
    The Expression: \bm\S*e\b
    make
    maze
    manage
    measure

示例 3

这个例子替换了额外的空白符:

using System;
    using System.Text.RegularExpressions;

    namespace RegExApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input = "Hello   World   ";
                string pattern = "\\s+";
                string replacement = " ";
                Regex rgx = new Regex(pattern);
                string result = rgx.Replace(input,replacement);

                Console.WriteLine("Original String: {0}",input);
                Console.WriteLine("Replacement String: {0}",result);    
                Console.ReadKey();
            }
        }
    }

编译执行上述代码,得到如下结果:

Original String: Hello World   
    Replacement String: Hello World

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐