提供正则表达式相关类型
System.Text.RegularExpressions
定义
描述
包含正则表达式引擎和相关类,用于字符串模式匹配和替换操作
参数
参数名 | 类型 | 描述 |
---|---|---|
pattern | string | 正则表达式模式 |
input | string | 要匹配的输入字符串 |
返回值
返回值 | 类型 | 描述 |
---|---|---|
match | Match | 匹配结果对象 |
示例
示例代码,匹配邮箱地址:
using System.Text.RegularExpressions;
class Program {
static void Main() {
string email = "test@example.com";
bool isValid = Regex.IsMatch(email, @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$");
Console.WriteLine(isValid); // 输出: True
}
}