C#【中级篇】 C# 正则表达式
精品文章:
- C#正则表达式大全 https://www.cnblogs.com/hehehehehe/p/6043710.html
- 微软文档 https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/regular-expressions
前言
正则表达式 是一种匹配输入文本的模式。
.Net 框架提供了允许这种匹配的正则表达式引擎。
模式由一个或多个字符、运算符和结构组成。
如果你还不理解正则表达式,可以查看 正则表达式 - 教程。
一、定义正则表达式
下面列出了用于定义正则表达式的各种类别的字符、运算符和结构。
- 字符转义
- 字符类
- 定位点
- 分组构造
- 限定符
- 反向引用构造
- 备用构造
- 替换
- 杂项构造
字符转义
正则表达式中的反斜杠字符(\)指示其后跟的字符是特殊字符,或应按原义解释该字符。
下表列出了转义字符:
字符类
字符类与一组字符中的任何一个字符匹配。
下表列出了字符类:
定位点
定位点或原子零宽度断言会使匹配成功或失败,具体取决于字符串中的当前位置,但它们不会使引擎在字符串中前进或使用字符。
下表列出了定位点:
分组构造
分组构造描述了正则表达式的子表达式,通常用于捕获输入字符串的子字符串。
这一部分比较难于理解,可以阅读 正则表达式-选择 、正则表达式的先行断言(lookahead)和后行断言(lookbehind) 帮助理解。
下表列出了分组构造:
实例:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string input = "1851 1999 1950 1905 2003";
string pattern = @"(?<=19)\d{2}\b";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine(match.Value);
Console.ReadLine();
}
}
运行结果:
99
50
05
限定符
限定符指定在输入字符串中必须存在上一个元素(可以是字符、组或字符类)的多少个实例才能出现匹配项。 限定符包括下表中列出的语言元素。
下表列出了限定符:
反向引用构造
反向引用允许在同一正则表达式中随后标识以前匹配的子表达式。
下表列出了反向引用构造:
备用构造
备用构造用于修改正则表达式以启用 either/or 匹配。
下表列出了备用构造:
替换
替换是替换模式中使用的正则表达式。
下表列出了用于替换的字符:
杂项构造
下表列出了各种杂项构造:
二、Regex 类
Regex 类用于表示一个正则表达式。
下表列出了 Regex 类中一些常用的方法:
如需了解 Regex 类的完整的属性列表,请参阅微软的 C# 文档。
三、实例
实例 1【匹配了以 ‘S’ 开头的单词】
下面的实例匹配了以 ‘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’ 结尾的单词】
下面的实例匹配了以 ‘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
总结
- 大致明白正则表达式的意思,及实际使用场景。
- 在需要用的时候现查即可,无需按顺序学习,网上有好多资料
- 多收集些实例,实际用到时,可以快速复制使用