C#中使用正则表达式主要是通过Regex类来实现,使用前需要包含命名空间:using System.Text.RegularExpressions
1 Regex类中的主要方法
c#中通过Regex类使用正则主要有两种方法,一种是通过创建Regex对象,另外一种是在临时使用正则的情景下,无需创建Regex实例,直接使用包装器函数Regex.函数成员
1.1 创建Regex对象
Regex myRegex = new Regex(string pattern);
— string pattern 表示设计的正则表达式
— 输入正则表达式时,可以在双引号前加上@符号作为开头,这样就不需要再对其中的转义字符加上\
主要方法的使用:
(1) IsMatch()
public bool IsMatch( string input )
指示 Regex 构造函数中指定的正则表达式是否在指定的输入字符串中找到了匹配项。
例如:验证输入的电话号码格式是否正确
static void Main(string[] args)
{
string input = "027-51272953";
string pattern = @"^(\d{3,4}-)\d{7,8}$";
Regex myRegex = new Regex(pattern);
if(myRegex.IsMatch(input))
{
Console.WriteLine("电话号码格式正确");
}
Console.ReadKey();
}
(2) Match()
public Match Match( string input )
在输入的字符串中搜索Regex 构造函数中指定的正则表达式的第一个匹配项
(3) Matches()
public MatchCollection Matches( string input )
在指定的输入字符串中搜索正则表达式的所有匹配项
例如:从字符串信息中提取每个人的姓名及性别
static void Main(string[] args)
{
string input = "姓名:JX 性别:女 学历:专科 姓名:LL 性别:男 学历:本科";
string pattern = @"姓名:\s*(\S+)\s*性别:\s*(\w)";
Regex myRegex = new Regex(pattern);
MatchCollection collection = myRegex.Matches(input);
foreach(Match m in collection)
{
Console.WriteLine("姓名:{0} 性别:{1}",m.Groups[1], m.Groups[2]);
}
Console.ReadKey();
}
结果:
姓名:JX 性别:女
姓名:LL 性别:男
Match.Groups[ ]返回的是对应子表达式的匹配信息,下标数字从1开始。其中m.Groups[1]返回的是正则表达式中第一个子表达式(\S+)的匹配信息,m.Groups[2]返回的则是第二个子表达式(\w)的匹配信息,而m.Groups[0]则返回的是整个正则表达式的匹配结果。
(4) Replace()
public string Replace( string input, string replacement )
在指定的输入字符串中,把所有匹配正则表达式模式的所有匹配的字符串替换为指定的替换字符串
例如:去除字符间多余的空格
static void Main(string[] args)
{
string input = "Hello World";
string pattern = @"\s{2,}";
Regex myRegex = new Regex(pattern);
string output = myRegex.Replace(input, " ");
Console.WriteLine("{0}", output);
Console.ReadKey();
}
结果:
Hello World
(5)Split()
public string[] Split( string input )
把输入字符串分割为子字符串数组,根据在 Regex 构造函数中指定的正则表达式模式定义的位置进行分割
例如:把字符串中的每个单词提取出来
static void Main(string[] args)
{
string input = "After all, tomorrow is another day";
string pattern = @"\s+|,\s*";
Regex myRegex = new Regex(pattern);
string[] output = myRegex.Split(input);
foreach (string s in output)
Console.WriteLine("{0}", s);
Console.ReadKey();
}
结果为:
After
all
tomorrow
is
another
day
1.2 Regex.函数成员
这种无需创建Regex实例的方式下,在调用方法成员时,正则表达式在方法成员的参数pattern中构建。
(1) public bool IsMatch( string input, string pattern)
(2) public Match Match( string input, string pattern)
(3) public MatchCollection Matches( string input , string pattern)
(4) public string Replace( string input, string pattern, string replacement )
(5) public string[] Split( string input, string pattern )
2 C#中的命名捕获
.net中对正则表达式支持命名捕获,即允许对子表达式进行命名。
语法:"(?<name>exp)"
匹配exp并捕获文本到名称为name的组里,通过Match.Groups["name"]取得分组值,而一般的子表达式形式"(exp)"则是自动分配到从1开始的组中,通过Match.Groups[1]取得对应分组值。
其他应用:
"\k<name>" 引用回溯引用
"${name}" 替换模式中
例如:从字符串信息中提取每个人的姓名及性别
static void Main(string[] args)
{
string input = "姓名:JX 性别:女 学历:专科 姓名:LL 性别:男 学历:本科";
string pattern = @"姓名:\s*(?<name>\S+)\s*性别:\s*(?<sex>\w)";
Regex myRegex = new Regex(pattern);
MatchCollection collection = myRegex.Matches(input);
foreach (Match m in collection)
{
Console.WriteLine("姓名:{0} 性别:{1}", m.Groups["name"], m.Groups["sex"]);
}
Console.ReadKey();
}
结果为:
姓名:JX 性别:女
姓名:LL 性别:男