Skip to content

Instantly share code, notes, and snippets.

@maate
Created December 18, 2011 10:38
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 maate/1492998 to your computer and use it in GitHub Desktop.
Save maate/1492998 to your computer and use it in GitHub Desktop.
CaptureEvaluator for .NET Regex Engine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
namespace RegexExtensions.RecursiveReplace
{
public static class RegexExtensions
{
[Serializable]
public delegate string CaptureEvaluator(Capture capture);
private static CaptureCollection getCapturesFromGroup(Match match, String groupName = null)
{
if (groupName != null)
return match.Groups[groupName].Captures;
else
return match.Captures;
}
public static String ReplaceRecursive(this Regex self, String input, CaptureEvaluator evaluator, String evaluateCapturesFromGroupName = null)
{
var sb = new StringBuilder();
sb.Append(input);
MatchCollection matches = self.Matches(input);
for(Int32 matchNumber = matches.Count - 1; matchNumber > -1; matchNumber--)
{
var match = matches[matchNumber];
var captures = getCapturesFromGroup(match, evaluateCapturesFromGroupName);
if (captures != null)
{
var capture = captures[0];
if (capture.Value != null)
{
var replaceWith = evaluator(capture);
if (replaceWith != null)
{
sb.Replace(capture.Value, replaceWith, capture.Index, capture.Length);
if (sb.ToString() == input)
continue;
var s = ReplaceRecursive(self, sb.ToString(), evaluator);
sb.Remove(0, sb.Length);
sb.Append(s);
}
}
}
}
return sb.ToString();
}
}
}
@maate
Copy link
Author

maate commented Dec 18, 2011

Test example:

public class when_working_with_the_regex_extensions : Specification
    {
        protected String Input;
        protected String Result;
        protected String Pattern;

        public when_working_with_the_regex_extensions()
        {
            Pattern = @"\((?>
 (?<OPEN>) \(
|
 \) (?<VALUE-OPEN>)
|
 [^()]?
)*
(?(OPEN)(?!))\)";
        }

        public void calculateCalculus()
        {
            var reg = new Regex(Pattern, RegexOptions.IgnorePatternWhitespace);
            Result = reg.ReplaceRecursive(Input, (p) =>
            {
                return new System.Data.DataTable().Compute(p.Value, null).ToString();
            }, "VALUE");
        }
    }

class and_computes_a_simple_calculus :
        when_working_with_the_regex_extensions
    {
        protected override void Because_of()
        {
            Input = "( ( 7 = ( 10 - 15 ) ) )";
            calculateCalculus();
        }

        [Test]
        public void then_the_result_should_be_a_reduced_calculus()
        {
            Result.ShouldEqual("False");
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment