diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f6b732..db1c35f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added GetAttributeAsEnum xml utils method - Adding short parsing methods to XML utils - Added methods to IcdUriBuilder for appending path + - Added RegexUtils method for replacing a single group in a match ## [8.0.0] - 2018-11-20 ### Added diff --git a/ICD.Common.Utils.Tests/RegexUtilsTest.cs b/ICD.Common.Utils.Tests/RegexUtilsTest.cs new file mode 100644 index 0000000..30f0904 --- /dev/null +++ b/ICD.Common.Utils.Tests/RegexUtilsTest.cs @@ -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\(""(?(\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(); + } + } +} diff --git a/ICD.Common.Utils/RegexUtils.cs b/ICD.Common.Utils/RegexUtils.cs index 1b294d2..727de4d 100644 --- a/ICD.Common.Utils/RegexUtils.cs +++ b/ICD.Common.Utils/RegexUtils.cs @@ -1,4 +1,7 @@ -using System.Text.RegularExpressions; +using System; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; namespace ICD.Common.Utils { @@ -30,5 +33,55 @@ namespace ICD.Common.Utils match = Regex.Match(input, pattern, options); return match.Success; } + + /// + /// Uses the pattern to replace the specified group with the provided replacement string. + /// + /// + /// + /// + /// + /// + public static string ReplaceGroup(string input, string pattern, string groupName, string replacement) + { + return ReplaceGroup(input, pattern, groupName, match => replacement); + } + + /// + /// Uses the pattern to replace the specified group with the provided replacement string. + /// + /// + /// + /// + /// + /// + public static string ReplaceGroup(string input, string pattern, string groupName, Func 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()) + { + 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); + } } }