C#正则表达式使用
1、命名空间
namespace System.Text.RegularExpressions
2、构造函数
// 摘要: // 初始化 System.Text.RegularExpressions.Regex 类的新实例。 protected Regex(); // // 摘要: // 针对指定的正则表达式初始化 System.Text.RegularExpressions.Regex 类的新实例。 // // 参数: // pattern: // 要匹配的正则表达式模式。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // pattern 为 null。 public Regex(string pattern); // // 摘要: // 使用序列化数据初始化 System.Text.RegularExpressions.Regex 类的新实例。 // // 参数: // info: // 包含序列化模式和 System.Text.RegularExpressions.RegexOptions 信息的对象。 // // context: // 此序列化的目标。 (未使用此参数;指定 null。) // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // info 包含的模式为 null。 // // System.ArgumentOutOfRangeException: // info 包含无效的 System.Text.RegularExpressions.RegexOptions 标志。 protected Regex(SerializationInfo info, StreamingContext context); // // 摘要: // 用修改模式的选项为指定的正则表达式初始化并编译 System.Text.RegularExpressions.Regex 类的一个新实例。 // // 参数: // pattern: // 要匹配的正则表达式模式。 // // options: // 用于修改正则表达式的枚举值的按位组合。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // pattern 为 null。 // // System.ArgumentOutOfRangeException: // options 包含无效标志。 public Regex(string pattern, RegexOptions options); // // 摘要: // 用修改模式的选项和指定在超时前多久应进行匹配尝试的模式匹配方法值的指定正则表达式来初始化 System.Text.RegularExpressions.Regex // 类的新实例。 // // 参数: // pattern: // 要匹配的正则表达式模式。 // // options: // 用于修改正则表达式的枚举值的按位组合。 // // matchTimeout: // 超时间隔,或 System.Text.RegularExpressions.Regex.InfiniteMatchTimeout 指示该方法不应超时。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // pattern 为 null。 // // System.ArgumentOutOfRangeException: // options 不是有效的 System.Text.RegularExpressions.RegexOptions 值。 - 或 - matchTimeout // 为负或大于约24天。 public Regex(string pattern, RegexOptions options, TimeSpan matchTimeout);
3、相关参数说明
查询时匹配选项
// 摘要: // 提供用于设置正则表达式选项的枚举值。 [Flags] public enum RegexOptions { // 摘要: // 指定不设置任何选项。 None = 0, // // 摘要: // 指定不区分大小写的匹配。 IgnoreCase = 1, // // 摘要: // 多行模式。 更改 ^ 和 $ 的含义,使它们分别在任意一行的行首和行尾匹配,而不仅仅在整个字符串的开头和结尾匹配。 Multiline = 2, // // 摘要: // 指定唯一有效的捕获是显式命名或编号的 (?<name>…) 形式的组。 这使未命名的圆括号可以充当非捕获组,并且不会使表达式的语法 (?:...) // 显得笨拙。 ExplicitCapture = 4, // // 摘要: // 指定将正则表达式编译为程序集。 这会产生更快的执行速度,但会增加启动时间。 在调用 System.Text.RegularExpressions.Regex.CompileToAssembly(System.Text.RegularExpressions.RegexCompilationInfo[],System.Reflection.AssemblyName) // 方法时,不应将此值分配给 System.Text.RegularExpressions.RegexCompilationInfo.Options // 属性。 Compiled = 8, // // 摘要: // 指定单行模式。 更改点 (.) 的含义,以使它与每个字符(而不是除 \n 之外的所有字符)匹配。 Singleline = 16, // // 摘要: // 消除模式中的非转义空白并启用由 # 标记的注释。 但是,System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace // 值不会影响或消除字符类中的空白。 IgnorePatternWhitespace = 32, // // 摘要: // 指定搜索从右向左而不是从左向右进行。 RightToLeft = 64, // // 摘要: // 为表达式启用符合 ECMAScript 的行为。 该值只能与 System.Text.RegularExpressions.RegexOptions.IgnoreCase、System.Text.RegularExpressions.RegexOptions.Multiline // 和 System.Text.RegularExpressions.RegexOptions.Compiled 值一起使用。 该值与其他任何值一起使用均将导致异常。 ECMAScript = 256, // // 摘要: // 指定忽略语言中的区域性差异。 有关更多信息,请参见[<topic://cpconPerformingCulture-InsensitiveOperationsInRegularExpressionsNamespace>]。 CultureInvariant = 512, }
4、常用方法
4.1、IsMatch
IsMatch有以下5个重载函数
// // 摘要: // 指示 System.Text.RegularExpressions.Regex 构造函数中指定的正则表达式在指定的输入字符串中是否找到了匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // 返回结果: // 如果正则表达式找到匹配项,则为 true;否则,为 false。 // // 异常: // System.ArgumentNullException: // input 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public bool IsMatch(string input); // // 摘要: // 指示 System.Text.RegularExpressions.Regex 构造函数中指定的正则表达式在指定的输入字符串中,从该字符串中的指定起始位置开始是否找到了匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // startat: // 开始搜索的字符位置。 // // 返回结果: // 如果正则表达式找到匹配项,则为 true;否则,为 false。 // // 异常: // System.ArgumentNullException: // input 为 null。 // // System.ArgumentOutOfRangeException: // startat 小于零,或者大于 input 的长度。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public bool IsMatch(string input, int startat); // // 摘要: // 指示所指定的正则表达式在指定的输入字符串中是否找到了匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // 返回结果: // 如果正则表达式找到匹配项,则为 true;否则,为 false。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static bool IsMatch(string input, string pattern); // // 摘要: // 指示所指定的正则表达式是否使用指定的匹配选项在指定的输入字符串中找到了匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // 返回结果: // 如果正则表达式找到匹配项,则为 true;否则,为 false。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 // // System.ArgumentOutOfRangeException: // options 不是有效的 System.Text.RegularExpressions.RegexOptions 值。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static bool IsMatch(string input, string pattern, RegexOptions options); // // 摘要: // 指示所指定的正则表达式是否使用指定的匹配选项在指定的输入字符串中找到了匹配项和超时间隔。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // matchTimeout: // 超时间隔,或 System.Text.RegularExpressions.Regex.InfiniteMatchTimeout 指示该方法不应超时。 // // 返回结果: // 如果正则表达式找到匹配项,则为 true;否则,为 false。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 // // System.ArgumentOutOfRangeException: // options 不是有效的 System.Text.RegularExpressions.RegexOptions 值。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 public static bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
使用示例:
static void Main(string[] args) { //匹配域名 //实例方法 Regex regex = new Regex("(http://(www\\.)?)?\\w\\.(com|cn)"); string srcStr = "http://www.baidu.com"; bool isMatch = regex.IsMatch(srcStr); Console.WriteLine(isMatch); srcStr = "百度http://www.baidu"; int stratLoc = 14; isMatch = regex.IsMatch(srcStr, stratLoc); Console.WriteLine(isMatch); //静态方法,共有3个重载 isMatch = Regex.IsMatch(srcStr, "(http://(www\\.)?)?\\w\\.(com|cn)", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(10)); Console.WriteLine(isMatch); Console.ReadKey(); }
4.2、Match
Match有6个重载函数
// // 摘要: // 在指定的输入字符串中搜索 System.Text.RegularExpressions.Regex 构造函数中指定的正则表达式的第一个匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // 返回结果: // 一个包含有关匹配的信息的对象。 // // 异常: // System.ArgumentNullException: // input 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public Match Match(string input); // // 摘要: // 从输入字符串中的指定起始位置开始,在该字符串中搜索正则表达式的第一个匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // startat: // 开始搜索的字符位置(从零开始)。 // // 返回结果: // 一个包含有关匹配的信息的对象。 // // 异常: // System.ArgumentNullException: // input 为 null。 // // System.ArgumentOutOfRangeException: // startat 小于零,或者大于 input 的长度。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public Match Match(string input, int startat); // // 摘要: // 在指定的输入字符串中搜索指定的正则表达式的第一个匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // 返回结果: // 一个包含有关匹配的信息的对象。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static Match Match(string input, string pattern); // // 摘要: // 从指定的起始位置开始,在输入字符串中搜索正则表达式的第一个匹配项,并且仅搜索指定数量的字符。 // // 参数: // input: // 要搜索匹配项的字符串。 // // beginning: // 输入字符串中开始搜索的最左侧的位置(从零开始)。 // // length: // 子字符串中包含在搜索中的字符数。 // // 返回结果: // 一个包含有关匹配的信息的对象。 // // 异常: // System.ArgumentNullException: // input 为 null。 // // System.ArgumentOutOfRangeException: // beginning 小于零,或者大于 input 的长度。 - 或 - length 小于零,或者大于 input 的长度。 - 或 - beginning+length– // 1 标识在 input 范围外的一个位置。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public Match Match(string input, int beginning, int length); // // 摘要: // 使用指定的匹配选项在输入字符串中搜索指定的正则表达式的第一个匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // 返回结果: // 一个包含有关匹配的信息的对象。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 // // System.ArgumentOutOfRangeException: // options 不是 System.Text.RegularExpressions.RegexOptions 值的有效按位组合。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static Match Match(string input, string pattern, RegexOptions options); // // 摘要: // 使用指定的匹配选项和超时间隔在输入字符串中搜索指定的正则表达式的第一个匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // matchTimeout: // 超时间隔,或 System.Text.RegularExpressions.Regex.InfiniteMatchTimeout 指示该方法不应超时。 // // 返回结果: // 一个包含有关匹配的信息的对象。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 // // System.ArgumentOutOfRangeException: // options 不是 System.Text.RegularExpressions.RegexOptions 值的有效按位组合。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static Match Match(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
使用示例:
Regex regex = new Regex("(http://(www\\.)?)?\\w+\\.(com|cn)+"); string srcStr = "http://www.baidu.com,谷歌google.com"; //实例方法 Match match = regex.Match(srcStr); match = regex.Match(srcStr, 0); //指定开始位置 match = regex.Match(srcStr, 0, srcStr.Length); //指定开始结束位置 if (match.Success) { Console.WriteLine(match.Value); } //静态方法 match = Regex.Match(srcStr, "(http://(www\\.)?)?\\w+\\.(com|cn)+", RegexOptions.IgnoreCase); if (match.Success) { Console.WriteLine(match.Value); }
4.3、Matchs方法
Matchs方法有5个重载
// // 摘要: // 在指定的输入字符串中搜索正则表达式的所有匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // 返回结果: // 搜索操作找到的 System.Text.RegularExpressions.Match 对象的集合。 如果未找到任何匹配,则该方法将返回空集合对象。 // // 异常: // System.ArgumentNullException: // input 为 null。 public MatchCollection Matches(string input); // // 摘要: // 从字符串中的指定起始位置开始,在指定的输入字符串中搜索正则表达式的所有匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // startat: // 在输入字符串中开始搜索的字符位置。 // // 返回结果: // 搜索操作找到的 System.Text.RegularExpressions.Match 对象的集合。 如果未找到任何匹配,则该方法将返回空集合对象。 // // 异常: // System.ArgumentNullException: // input 为 null。 // // System.ArgumentOutOfRangeException: // startat 小于零,或者大于 input 的长度。 public MatchCollection Matches(string input, int startat); // // 摘要: // 在指定的输入字符串中搜索指定的正则表达式的所有匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // 返回结果: // 搜索操作找到的 System.Text.RegularExpressions.Match 对象的集合。 如果未找到任何匹配,则该方法将返回空集合对象。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 public static MatchCollection Matches(string input, string pattern); // // 摘要: // 使用指定的匹配选项在指定的输入字符串中搜索指定的正则表达式的所有匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // options: // 枚举值的按位组合,这些枚举值指定用于匹配的选项。 // // 返回结果: // 搜索操作找到的 System.Text.RegularExpressions.Match 对象的集合。 如果未找到任何匹配,则该方法将返回空集合对象。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input 或 pattern 为 null。 // // System.ArgumentOutOfRangeException: // options 不是 System.Text.RegularExpressions.RegexOptions 值的有效按位组合。 public static MatchCollection Matches(string input, string pattern, RegexOptions options); // // 摘要: // 使用指定的匹配选项和超时间隔在指定的输入字符串中搜索指定的正则表达式的所有匹配项。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // options: // 枚举值的按位组合,这些枚举值指定用于匹配的选项。 // // matchTimeout: // 超时间隔,或 System.Text.RegularExpressions.Regex.InfiniteMatchTimeout 指示该方法不应超时。 // // 返回结果: // 搜索操作找到的 System.Text.RegularExpressions.Match 对象的集合。 如果未找到任何匹配,则该方法将返回空集合对象。 public static MatchCollection Matches(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
Matchs使用示例:
Regex regex = new Regex("(http://(www\\.)?)?\\w+\\.(com|cn)+"); string srcStr = "http://www.baidu.com,谷歌google.com"; //实例方法 MatchCollection matchCollec = regex.Matches(srcStr); for (int i = 0; i < matchCollec.Count; i++) { var match = matchCollec[i]; if (match.Success) { Console.WriteLine(match.Value); } } //静态方法 matchCollec = Regex.Matches(srcStr, "(http://(www\\.)?)?\\w+\\.(com|cn)+", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(10)); for (int i = 0; i < matchCollec.Count; i++) { var match = matchCollec[i]; if (match.Success) { Console.WriteLine(match.Value); } } Console.ReadKey();
4.4、Repalce方法
Replace方法共12个重载
// // 摘要: // 在指定的输入字符串内,使用 System.Text.RegularExpressions.MatchEvaluator 委托返回的字符串替换与指定正则表达式匹配的所有字符串。 // // 参数: // input: // 要搜索匹配项的字符串。 // // evaluator: // 一个自定义方法,它检查每个匹配项,并返回原始匹配字符串或替换字符串。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentNullException: // input 或 evaluator 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public string Replace(string input, MatchEvaluator evaluator); // // 摘要: // 在指定的输入字符串内,使用指定的替换字符串替换与某个正则表达式模式匹配的所有的字符串。 // // 参数: // input: // 要搜索匹配项的字符串。 // // replacement: // 替换字符串。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentNullException: // input 或 replacement 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public string Replace(string input, string replacement); // // 摘要: // 在指定的输入字符串内,使用 System.Text.RegularExpressions.MatchEvaluator 委托返回的字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。 // // 参数: // input: // 要搜索匹配项的字符串。 // // evaluator: // 一个自定义方法,它检查每个匹配项,并返回原始匹配字符串或替换字符串。 // // count: // 进行替换的最大次数。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentNullException: // input 或 evaluator 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public string Replace(string input, MatchEvaluator evaluator, int count); // // 摘要: // 在指定输入字符串内,使用指定替换字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。 // // 参数: // input: // 要搜索匹配项的字符串。 // // replacement: // 替换字符串。 // // count: // 可进行替换的最大次数。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentNullException: // input 或 replacement 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public string Replace(string input, string replacement, int count); // // 摘要: // 在指定的输入字符串内,使用 System.Text.RegularExpressions.MatchEvaluator 委托返回的字符串替换与指定正则表达式匹配的所有字符串。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // evaluator: // 一个自定义方法,它检查每个匹配项,并返回原始匹配字符串或替换字符串。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input、pattern 或 evaluator 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static string Replace(string input, string pattern, MatchEvaluator evaluator); // // 摘要: // 在指定的输入字符串内,使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // replacement: // 替换字符串。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input、pattern 或 replacement 为 null。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static string Replace(string input, string pattern, string replacement); // // 摘要: // 在指定的输入子字符串内,使用 System.Text.RegularExpressions.MatchEvaluator 委托返回的字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。 // // 参数: // input: // 要搜索匹配项的字符串。 // // evaluator: // 一个自定义方法,它检查每个匹配项,并返回原始匹配字符串或替换字符串。 // // count: // 进行替换的最大次数。 // // startat: // 输入字符串中开始执行搜索的字符位置。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentNullException: // input 或 evaluator 为 null。 // // System.ArgumentOutOfRangeException: // startat 小于零,或者大于 input 的长度。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public string Replace(string input, MatchEvaluator evaluator, int count, int startat); // // 摘要: // 在指定输入子字符串内,使用指定替换字符串替换与某个正则表达式模式匹配的字符串(其数目为指定的最大数目)。 // // 参数: // input: // 要搜索匹配项的字符串。 // // replacement: // 替换字符串。 // // count: // 可进行替换的最大次数。 // // startat: // 输入字符串中开始执行搜索的字符位置。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentNullException: // input 或 replacement 为 null。 // // System.ArgumentOutOfRangeException: // startat 小于零,或者大于 input 的长度。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public string Replace(string input, string replacement, int count, int startat); // // 摘要: // 在指定的输入字符串内,使用 System.Text.RegularExpressions.MatchEvaluator 委托返回的字符串替换与指定正则表达式匹配的所有字符串。 // 指定的选项将修改匹配操作。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // evaluator: // 一个自定义方法,它检查每个匹配项,并返回原始匹配字符串或替换字符串。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input、pattern 或 evaluator 为 null。 // // System.ArgumentOutOfRangeException: // options 不是 System.Text.RegularExpressions.RegexOptions 值的有效按位组合。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options); // // 摘要: // 在指定的输入字符串内,使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。 指定的选项将修改匹配操作。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // replacement: // 替换字符串。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input、pattern 或 replacement 为 null。 // // System.ArgumentOutOfRangeException: // options 不是 System.Text.RegularExpressions.RegexOptions 值的有效按位组合。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static string Replace(string input, string pattern, string replacement, RegexOptions options); // // 摘要: // 在指定的输入字符串内,使用 System.Text.RegularExpressions.MatchEvaluator 委托返回的字符串替换与指定正则表达式匹配的所有字符串。 // 其他参数指定修改匹配的操作,如果未找到匹配项,则修改超时间隔。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // evaluator: // 一个自定义方法,它检查每个匹配项,并返回原始匹配字符串或替换字符串。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // matchTimeout: // 超时间隔,或 System.Text.RegularExpressions.Regex.InfiniteMatchTimeout 指示该方法不应超时。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input、pattern 或 evaluator 为 null。 // // System.ArgumentOutOfRangeException: // options 不是 System.Text.RegularExpressions.RegexOptions 值的有效按位组合。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options, TimeSpan matchTimeout); // // 摘要: // 在指定的输入字符串内,使用指定的替换字符串替换与指定正则表达式匹配的所有字符串。 其他参数指定修改匹配的操作,如果未找到匹配项,则修改超时间隔。 // // 参数: // input: // 要搜索匹配项的字符串。 // // pattern: // 要匹配的正则表达式模式。 // // replacement: // 替换字符串。 // // options: // 枚举值的一个按位组合,这些枚举值提供匹配选项。 // // matchTimeout: // 超时间隔,或 System.Text.RegularExpressions.Regex.InfiniteMatchTimeout 指示该方法不应超时。 // // 返回结果: // 一个与输入字符串基本相同的新字符串,唯一的差别在于,其中的每个匹配字符串已被替换字符串代替。 // // 异常: // System.ArgumentException: // 出现正则表达式分析错误。 // // System.ArgumentNullException: // input、pattern 或 replacement 为 null。 // // System.ArgumentOutOfRangeException: // options 不是 System.Text.RegularExpressions.RegexOptions 值的有效按位组合。 // // System.Text.RegularExpressions.RegexMatchTimeoutException: // 发生超时。 有关超时的更多信息,请参见“备注”节。 public static string Replace(string input, string pattern, string replacement, RegexOptions options, TimeSpan matchTimeout);
使用示例:
Regex regex = new Regex("(http://(www\\.)?)?\\w+\\.(com|cn)+"); string srcStr = "http://www.baidu.com,谷歌google.com"; //示例方法 string dest = regex.Replace(srcStr, AddHttp); Console.WriteLine(dest); //静态方法 dest = Regex.Replace(srcStr, "(http://(www\\.)?)?\\w+\\.(com|cn)+", AddHttp); Console.WriteLine(dest); static string AddHttp(Match match) { if (match.Success && !match.Value.StartsWith("http://")) { return "http://" + match.Value; } return match.Value; }
全部代码:
class Program { static void Main(string[] args) { #region IsMatch方法示例 ////匹配域名 ////实例方法 //Regex regex = new Regex("(http://(www\\.)?)?\\w\\.(com|cn)"); //string srcStr = "http://www.baidu.com"; //bool isMatch = regex.IsMatch(srcStr); //Console.WriteLine(isMatch); //srcStr = "百度http://www.baidu"; //int stratLoc = 14; //isMatch = regex.IsMatch(srcStr, stratLoc); //Console.WriteLine(isMatch); ////静态方法,共有3个重载 //isMatch = Regex.IsMatch(srcStr, "(http://(www\\.)?)?\\w\\.(com|cn)", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(10)); //Console.WriteLine(isMatch); #endregion #region Match方法示例 //Regex regex = new Regex("(http://(www\\.)?)?\\w+\\.(com|cn)+"); //string srcStr = "http://www.baidu.com,谷歌google.com"; ////实例方法 //Match match = regex.Match(srcStr); //match = regex.Match(srcStr, 0); //指定开始位置 //match = regex.Match(srcStr, 0, srcStr.Length); //指定开始结束位置 //if (match.Success) //{ // Console.WriteLine(match.Value); //} ////静态方法 //match = Regex.Match(srcStr, "(http://(www\\.)?)?\\w+\\.(com|cn)+", RegexOptions.IgnoreCase); //if (match.Success) //{ // Console.WriteLine(match.Value); //} #endregion #region Matchs 方法 //Regex regex = new Regex("(http://(www\\.)?)?\\w+\\.(com|cn)+"); //string srcStr = "http://www.baidu.com,谷歌google.com"; ////实例方法 //MatchCollection matchCollec = regex.Matches(srcStr); //for (int i = 0; i < matchCollec.Count; i++) //{ // var match = matchCollec[i]; // if (match.Success) // { // Console.WriteLine(match.Value); // } //} ////静态方法 //matchCollec = Regex.Matches(srcStr, "(http://(www\\.)?)?\\w+\\.(com|cn)+", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(10)); //for (int i = 0; i < matchCollec.Count; i++) //{ // var match = matchCollec[i]; // if (match.Success) // { // Console.WriteLine(match.Value); // } //} //Console.ReadKey(); #endregion #region replace 方法示例 Regex regex = new Regex("(http://(www\\.)?)?\\w+\\.(com|cn)+"); string srcStr = "http://www.baidu.com,谷歌google.com"; //示例方法 string dest = regex.Replace(srcStr, AddHttp); Console.WriteLine(dest); //静态方法 dest = Regex.Replace(srcStr, "(http://(www\\.)?)?\\w+\\.(com|cn)+", AddHttp); Console.WriteLine(dest); #endregion Console.ReadKey(); } static string AddHttp(Match match) { if (match.Success && !match.Value.StartsWith("http://")) { return "http://" + match.Value; } return match.Value; } }

浙公网安备 33010602011771号