Created
October 4, 2021 19:38
-
-
Save otuncelli/158bafc0240d84fdd407b99ceffb3555 to your computer and use it in GitHub Desktop.
Regex object that initialized and parsed when needed.
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
using System; | |
using System.Text.RegularExpressions; | |
namespace Tools | |
{ | |
public class LazyRegex | |
{ | |
private readonly Lazy<Regex> regex; | |
private Regex Re => regex.Value; | |
/// <inheritdoc cref="Regex(string)"/> | |
public LazyRegex(string pattern) | |
: this(pattern, RegexOptions.None) | |
{ | |
} | |
/// <inheritdoc cref="Regex(string, RegexOptions)"/> | |
public LazyRegex(string pattern, RegexOptions options) | |
: this(pattern, options, Regex.InfiniteMatchTimeout) | |
{ | |
} | |
/// <inheritdoc cref="Regex(string, RegexOptions, TimeSpan)"/> | |
public LazyRegex(string pattern, RegexOptions options, TimeSpan matchTimeout) | |
{ | |
regex = new Lazy<Regex>(() => new Regex(pattern, options, matchTimeout)); | |
} | |
#region IsMatch | |
/// <inheritdoc cref="Regex.IsMatch(string)"/> | |
public bool IsMatch(string input) => Re.IsMatch(input); | |
/// <inheritdoc cref="Regex.IsMatch(string, int)"/> | |
public bool IsMatch(string input, int startat) => Re.IsMatch(input, startat); | |
#endregion | |
#region Match | |
/// <inheritdoc cref="Regex.Match(string)"/> | |
public Match Match(string input) => Re.Match(input); | |
/// <inheritdoc cref="Regex.Match(string, int)"/> | |
public Match Match(string input, int startat) => Re.Match(input, startat); | |
/// <inheritdoc cref="Regex.Match(string, string)"/> | |
public Match Match(string input, int beginning, int length) => Re.Match(input, beginning, length); | |
#endregion | |
#region Replace | |
/// <inheritdoc cref="Regex.Replace(string, string)"/> | |
public string Replace(string input, string replacement) => Re.Replace(input, replacement); | |
/// <inheritdoc cref="Regex.Replace(string, string, int)"/> | |
public string Replace(string input, string replacement, int count) => Re.Replace(input, replacement, count); | |
/// <inheritdoc cref="Regex.Replace(string, string, int, int)"/> | |
public string Replace(string input, string replacement, int count, int startat) => Re.Replace(input, replacement, count, startat); | |
/// <inheritdoc cref="Regex.Replace(string, MatchEvaluator)"/> | |
public string Replace(string input, MatchEvaluator evaluator) => Re.Replace(input, evaluator); | |
/// <inheritdoc cref="Regex.Replace(string, MatchEvaluator, int)"/> | |
public string Replace(string input, MatchEvaluator evaluator, int count) => Re.Replace(input, evaluator, count); | |
/// <inheritdoc cref="Regex.Replace(string, MatchEvaluator, int, int)"/> | |
public string Replace(string input, MatchEvaluator evaluator, int count, int startat) => Re.Replace(input, evaluator, count, startat); | |
#endregion | |
#region Matches | |
/// <inheritdoc cref="Regex.Matches(string)"/> | |
public MatchCollection Matches(string input) => Re.Matches(input); | |
/// <inheritdoc cref="Regex.Matches(string, int)"/> | |
public MatchCollection Matches(string input, int startat) => Re.Matches(input, startat); | |
#endregion | |
#region Split | |
/// <inheritdoc cref="Regex.Split(string)"/> | |
public string[] Split(string input) => Re.Split(input); | |
/// <inheritdoc cref="Regex.Split(string, int)"/> | |
public string[] Split(string input, int count) => Re.Split(input, count); | |
/// <inheritdoc cref="Regex.Split(string, int, int)"/> | |
public string[] Split(string input, int count, int startat) => Re.Split(input, count, startat); | |
#endregion | |
#region Other | |
/// <inheritdoc cref="Regex.GetGroupNames"/> | |
public string[] GetGroupNames() => Re.GetGroupNames(); | |
/// <inheritdoc cref="Regex.GetGroupNumbers"/> | |
public int[] GetGroupNumbers() => Re.GetGroupNumbers(); | |
/// <inheritdoc cref="Regex.GroupNameFromNumber(int)"/> | |
public string GroupNameFromNumber(int i) => Re.GroupNameFromNumber(i); | |
/// <inheritdoc cref="Regex.GroupNumberFromName(string)"/> | |
public int GroupNumberFromName(string name) => Re.GroupNumberFromName(name); | |
#endregion | |
/// <inheritdoc cref="Regex.ToString"/> | |
public override string ToString() => Re.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment