Skip to content

Commit

Permalink
Regex patch.
Browse files Browse the repository at this point in the history
  • Loading branch information
houseofcat committed Nov 24, 2023
1 parent 0373c43 commit f630e8b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Mnemonic.Strings/Regex/RegexPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ public sealed record RegexPair
{
public System.Text.RegularExpressions.Regex Regex { get; private set; }
public string Replacement { get; private set; }
public int Count { get; private set; }

public RegexPair(string pattern, string replacement, RegexOptions? options = null)
public RegexPair(string pattern, string replacement, int count = 1, RegexOptions? options = null)
{
Regex = new System.Text.RegularExpressions.Regex(
RegexHelpers.BuildRegexExactPhrasePattern(pattern),
RegexHelpers.BuildRegexSubstringPattern(pattern),
options ?? RegexOptions.Singleline);

Replacement = replacement;
Count = count;
}

public bool IsMatch(ReadOnlySpan<char> input)
Expand All @@ -25,6 +27,6 @@ public bool IsMatch(ReadOnlySpan<char> input)

public string Replace(string input)
{
return Regex.Replace(input, Replacement);
return Regex.Replace(input, Replacement, Count);
}
}
5 changes: 5 additions & 0 deletions src/Mnemonic.Utilities/Helpers/RegexHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ namespace Mnemonic.Utilities.Helpers;

public static class RegexHelpers
{
public static string BuildRegexSubstringPattern(string input)
{
return Regex.Escape(input);
}

private static readonly string _exactMatchTemplate = "^{0}$";

public static string BuildRegexExactPhrasePattern(string input)
Expand Down

0 comments on commit f630e8b

Please sign in to comment.