mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Added RegexUtils method for replacing a single group in a match
This commit is contained in:
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
- Added GetAttributeAsEnum xml utils method
|
- Added GetAttributeAsEnum xml utils method
|
||||||
- Adding short parsing methods to XML utils
|
- Adding short parsing methods to XML utils
|
||||||
- Added methods to IcdUriBuilder for appending path
|
- Added methods to IcdUriBuilder for appending path
|
||||||
|
- Added RegexUtils method for replacing a single group in a match
|
||||||
|
|
||||||
## [8.0.0] - 2018-11-20
|
## [8.0.0] - 2018-11-20
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
38
ICD.Common.Utils.Tests/RegexUtilsTest.cs
Normal file
38
ICD.Common.Utils.Tests/RegexUtilsTest.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Tests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public sealed class RegexUtilsTest
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public static void MatchesTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public static void MatchesOptionsTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase(@"[assembly: AssemblyFileVersion(""1.2.3"")][assembly: AssemblyFileVersion(""1.2.3"")]",
|
||||||
|
@"[assembly: AssemblyFileVersion(""2.0.3.0"")][assembly: AssemblyFileVersion(""2.0.3.0"")]",
|
||||||
|
@"AssemblyFileVersion\(""(?<version>(\d+\.?){4})""\)",
|
||||||
|
"version",
|
||||||
|
"1.2.3")]
|
||||||
|
public static void ReplaceGroupTest(string expected, string input, string pattern, string groupName,
|
||||||
|
string replacement)
|
||||||
|
{
|
||||||
|
string result = RegexUtils.ReplaceGroup(input, pattern, groupName, replacement);
|
||||||
|
Assert.AreEqual(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public static void ReplaceGroupFuncTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace ICD.Common.Utils
|
namespace ICD.Common.Utils
|
||||||
{
|
{
|
||||||
@@ -30,5 +33,55 @@ namespace ICD.Common.Utils
|
|||||||
match = Regex.Match(input, pattern, options);
|
match = Regex.Match(input, pattern, options);
|
||||||
return match.Success;
|
return match.Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Uses the pattern to replace the specified group with the provided replacement string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input"></param>
|
||||||
|
/// <param name="pattern"></param>
|
||||||
|
/// <param name="groupName"></param>
|
||||||
|
/// <param name="replacement"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string ReplaceGroup(string input, string pattern, string groupName, string replacement)
|
||||||
|
{
|
||||||
|
return ReplaceGroup(input, pattern, groupName, match => replacement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Uses the pattern to replace the specified group with the provided replacement string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input"></param>
|
||||||
|
/// <param name="pattern"></param>
|
||||||
|
/// <param name="groupName"></param>
|
||||||
|
/// <param name="replacement"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string ReplaceGroup(string input, string pattern, string groupName, Func<Match, string> replacement)
|
||||||
|
{
|
||||||
|
MatchEvaluator evaluator =
|
||||||
|
m =>
|
||||||
|
{
|
||||||
|
string replacementString = replacement(m);
|
||||||
|
|
||||||
|
Group group = m.Groups[groupName];
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
int previousCaptureEnd = 0;
|
||||||
|
foreach (Capture capture in group.Captures.Cast<Capture>())
|
||||||
|
{
|
||||||
|
int currentCaptureEnd = capture.Index + capture.Length - m.Index;
|
||||||
|
int currentCaptureLength = capture.Index - m.Index - previousCaptureEnd;
|
||||||
|
|
||||||
|
sb.Append(m.Value.Substring(previousCaptureEnd, currentCaptureLength));
|
||||||
|
sb.Append(replacementString);
|
||||||
|
|
||||||
|
previousCaptureEnd = currentCaptureEnd;
|
||||||
|
}
|
||||||
|
sb.Append(m.Value.Substring(previousCaptureEnd));
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
};
|
||||||
|
|
||||||
|
return Regex.Replace(input, pattern, evaluator);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user