C#正则表达式
Regex 类包含若干静态方法,使您无需显式创建 Regex 对象即可使用正则表达式。使用静态方法等效于构造 Regex 对象,使用该对象一次然后将其销毁。
Regex 类是不可变(只读)的,并且具有固有的线程安全性。可以在任何线程上创建 Regex 对象,并在线程间共享。
方法:Escape:通过替换为转义码来转义最小的元字符集(\、*、+、?、|、{、[、(、)、^、$、.、# 和空白)。
GetGroupNames:返回正则表达式的捕获组名数组。
GetGroupNumbers:返回与数组中的组名相对应的捕获组号的数组。
IsMatch:已重载。指示正则表达式在输入字符串中是否找到匹配项。
Match:在输入字符串中搜索正则表达式的匹配项,并将精确结果作为单个 Match对象返回。
Matchs:在输入字符串中搜索正则表达式的所有匹配项并返回所有成功的匹配,就像多次调用 Matchs 一样。
Replace:用指定的替换字符串替换由正则表达式定义的字符模式的所有匹配项。
Split:在由正则表达式匹配项定义的位置将输入字符串拆分为一个子字符串数组。
实例1:
using System.Text.RegularExpressions;
using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
string regularExpression = "s[ia]mple";
string inputString = "Is it simple sample?";
Match m = Regex.Match(inputString, regularExpression);
Console.WriteLine("Match = " + m.ToString());
Console.WriteLine("Next match = " + m.NextMatch().ToString());
}
}
}
using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
string regularExpression = "s[ia]mple";
string inputString = "Is it simple sample?";
Match m = Regex.Match(inputString, regularExpression);
Console.WriteLine("Match = " + m.ToString());
Console.WriteLine("Next match = " + m.NextMatch().ToString());
}
}
}
输出结果:
Match = simple
Next match = sample
实例2:
using System.Text.RegularExpressions;
using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
// Should match everything except the last two.
string regularExpression = @"\$(\d+)\.(\d\d)";
string inputString = "$1.57 $316.15 $19.30 $0.30 $0.00 $41.10 $5.1 $.5";
for (Match m = Regex.Match(inputString, regularExpression); m.Success; m = m.NextMatch())
{
GroupCollection gc = m.Groups;
Console.WriteLine("The number of captures: " + gc.Count);
// Group 0 is the entire matched string itself
// while Group 1 is the first group to be captured.
for (int i = 0; i < gc.Count; i++)
{
Group g = gc[i];
Console.WriteLine(g.Value);
}
}
}
}
}
using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
// Should match everything except the last two.
string regularExpression = @"\$(\d+)\.(\d\d)";
string inputString = "$1.57 $316.15 $19.30 $0.30 $0.00 $41.10 $5.1 $.5";
for (Match m = Regex.Match(inputString, regularExpression); m.Success; m = m.NextMatch())
{
GroupCollection gc = m.Groups;
Console.WriteLine("The number of captures: " + gc.Count);
// Group 0 is the entire matched string itself
// while Group 1 is the first group to be captured.
for (int i = 0; i < gc.Count; i++)
{
Group g = gc[i];
Console.WriteLine(g.Value);
}
}
}
}
}
输出结果:
The number of captures: 3
$1.57
1
57
The number of captures: 3
$316.15
316
15
The number of captures: 3
$19.30
19
30
The number of captures: 3
$0.30
0
30
The number of captures: 3
$0.00
0
00
The number of captures: 3
$41.10
41
10
实例3:
using System.Text.RegularExpressions;
using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
string regularExpression = @"(\s*)Dim\s+(\w+)\s+As\s+(\w+)";
string inputString = "Dim abc As Integer";
string replacement = "$1$3 $2;";
Console.WriteLine(Regex.Replace(inputString, regularExpression, replacement));
}
}
}
输出结果:using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
string regularExpression = @"(\s*)Dim\s+(\w+)\s+As\s+(\w+)";
string inputString = "Dim abc As Integer";
string replacement = "$1$3 $2;";
Console.WriteLine(Regex.Replace(inputString, regularExpression, replacement));
}
}
}
Integer abc;
实例4:
using System.Text.RegularExpressions;
using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
string regularExpression = @"^(?=.*?\d.*?\d)(?=.*?\w.*?\w)[\d\w]{8,20}$";
Console.WriteLine("Please input password for check:");
string inputString = Console.ReadLine();
if (inputString != "" && Regex.IsMatch(inputString, regularExpression))
{
Console.WriteLine("It's correct security password");
}
else
{
Console.WriteLine("It's incorrect password.");
}
Console.Read();
}
}
}
using System;
namespace RegularExpressionsSample
{
class Program
{
static void Main(string[] args)
{
string regularExpression = @"^(?=.*?\d.*?\d)(?=.*?\w.*?\w)[\d\w]{8,20}$";
Console.WriteLine("Please input password for check:");
string inputString = Console.ReadLine();
if (inputString != "" && Regex.IsMatch(inputString, regularExpression))
{
Console.WriteLine("It's correct security password");
}
else
{
Console.WriteLine("It's incorrect password.");
}
Console.Read();
}
}
}
输出结果:
Please input password for check:
abc4D5678
It's correct security password