Created
February 18, 2013 15:13
-
-
Save derantell/4978108 to your computer and use it in GitHub Desktop.
C# string extensions methods wrapping common regex tasks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringRegexExtensions{ | |
/// <summary> | |
/// Matches a string against a regular expression. | |
/// </summary> | |
/// <param name="self">The string self.</param> | |
/// <param name="pattern">The regular expression.</param> | |
/// <returns>True if it is match, otherwise false.</returns> | |
public static bool IsMatch(this string self, string pattern) { | |
return Regex.IsMatch(self, pattern); | |
} | |
/// <summary> | |
/// Matches a string against a regular expression. | |
/// </summary> | |
/// <param name="self">The string self.</param> | |
/// <param name="pattern">The regular expression.</param> | |
/// <param name="opt">Options.</param> | |
/// <returns>True if it is match, otherwise false.</returns> | |
public static bool IsMatch(this string self, string pattern, RegexOptions opt) { | |
return Regex.IsMatch(self, pattern, opt); | |
} | |
/// <summary> | |
/// Replaces occurrences defined by a regular expression. | |
/// </summary> | |
/// <param name="self">The string self.</param> | |
/// <param name="pattern">The regular expression.</param> | |
/// <param name="substitution">The substitution string.</param> | |
/// <returns>A string with replacements made.</returns> | |
public static string RegexReplace(this string self, string pattern, string substitution) { | |
return Regex.Replace(self, pattern, substitution); | |
} | |
/// <summary> | |
/// Replaces occurrences defined by a regular expression. | |
/// </summary> | |
/// <param name="self">The string self.</param> | |
/// <param name="pattern">The regular expression.</param> | |
/// <param name="substitution">The substitution string.</param> | |
/// <param name="opt">Options.</param> | |
/// <returns>A string with replacements made.</returns> | |
public static string RegexReplace(this string self, string pattern, string substitution, RegexOptions opt) { | |
return Regex.Replace(self, pattern, substitution, opt); | |
} | |
/// <summary> | |
/// Replaces occurrences defined by a regular expression. | |
/// </summary> | |
/// <param name="self">The string self.</param> | |
/// <param name="pattern">The regular expression.</param> | |
/// <param name="evaluator">A <see cref="MatchEvaluator"/> function that handles | |
/// the replacements.</param> | |
/// <returns>A string with replacements made.</returns> | |
public static string RegexReplace(this string self, string pattern, MatchEvaluator evaluator) { | |
return Regex.Replace(self, pattern, evaluator); | |
} | |
/// <summary> | |
/// Replaces occurrences defined by a regular expression. | |
/// </summary> | |
/// <param name="self">The string self.</param> | |
/// <param name="pattern">The regular expression.</param> | |
/// <param name="evaluator">A <see cref="MatchEvaluator"/> function that handles | |
/// the replacements.</param> | |
/// <param name="opt">Regex options.</param> | |
/// <returns>A string with replacements made.</returns> | |
public static string RegexReplace(this string self, string pattern, MatchEvaluator evaluator, RegexOptions opt) { | |
return Regex.Replace(self, pattern, evaluator, opt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment