• 首页
  • 新手教程库
  • 在线工具
  • 视频教程
  • 代码片段库
  • 更多功能
    • 在线测验
    • 手机APP
Hostwinds建站首选4刀起支持Linux/Win(支付宝)
PacificRack $12/年起三网线路优化(可支付宝)
国外VPS哪个最好?
搬瓦工VPS-新增日本机房限量方案年付69
HostDare CN2 GIA线路4k无压力
美国和欧洲vps
  1. 首页
  2. 新手教程库
  3. Microsoft技术教程
  4. C# - 正则表达式
C# - 概述 C# - 概述 C# - 环境 C# - 程序结构 C# - 基本语法 C# - 数据类型 C# - 类型转换 C# - 变量 C# - 常量和文字 C# - 运营商 C# - 决策 C# - 循环 C# - 封装 C# - 方法 C# - Nullables C# - 数组 C# - 字符串 C# - 结构 C# - 枚举 C# - 类 C# - 继承 C# - 多态性 C# - 运算符重载 C# - 接口 C# - 命名空间 C# - 预处理器指令 C# - 正则表达式 C# - 异常处理 C# - 文件I / O. C# - 属性 C# - 反思 C# - 属性 C# - 索引器 C# - 代表 C# - 活动 C# - 收藏 C# - 泛型 C# - 匿名方法 C# - 不安全代码 C# - 多线程 C# - 有用的资源
教 程 目 录
上一节
下一节  

正则表达式是一种可以与输入文本匹配的模式. .Net框架提供了允许这种匹配的正则表达式引擎.模式由一个或多个字符文字,运算符或构造组成.

用于定义正则表达式的构造

有各种类型的字符,运算符,以及允许您定义正则表达式的结构.单击以下链接以查找这些构造.

  • 字符转义

  • 字符类

  • 锚点

  • 分组构造

  • 量词

  • Ba ckreference构造

  • 轮换结构

  • 替代

  • 杂项构造

正则表达式类

正则表达式类用于表示正则表达式.它有以下常用方法 :

Sr.No.方法&说明
1

public bool IsMatch(string input)

指示Regex构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项.

2

public bool IsMatch(string input,int startat)

指示Regex构造函数中指定的正则表达式是否在指定的输入字符串中找到匹配项,从字符串中指定的起始位置开始.

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)

将输入字符串拆分为由子字符串数组定义的位置正则表达式在Regex构造函数中指定.

有关方法和属性的完整列表,请阅读有关C#的Microsoft文档.

示例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, 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 = "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
上一节
下一节  

相关新手教程:

Powershell教程
VBA教程
高级Excel教程
MFC 教程
C#教程
vb.net教程
Microsoft Project教程
LinQ教程
IT屋 ©2016-2020 京ICP备14011762号 鄂公网安备42018502004713号 站点地图 站点标签 意见&反馈  SiteMap <免责申明> 本站内容来源互联网,如果侵犯您的权益请联系我们删除.
  • 首页
  • 教程
  • 工具
  • 视频
  • 代码
  • 联系站长