Skip to content

Instantly share code, notes, and snippets.

@derantell
Created February 18, 2013 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derantell/4978108 to your computer and use it in GitHub Desktop.
Save derantell/4978108 to your computer and use it in GitHub Desktop.
C# string extensions methods wrapping common regex tasks.
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