Standard Simplified RegEx Implementation II

A simple modification to the code shown in the previous post can accommodate a new capability in the RegEx. Suppose that you want to add a new capability to the RegEx: given a digit N in the RegEx (from 0-9), match any number of N characters in the input. For example, for the RegEx: "a2c", the following are valid matches:

1. "abbc"

2. "cccc"

Whereas the string "abbbc" would be an invalid since between the 'a' and 'c' there are 3 characters instead of 2. The change in the code to implement this functionality in highlighted here. You can see that the structure of this code allows us relatively easily to add new functionalities to the RegEx. This is one of them, but one can imagine others such as "matching a minimum of N characters" instead of exactly N characters. Hope it is helpful - cheers, ACC.

private bool HasMatch(string str,
                        string regex,
                        int strEndIndex,
                        int strCurrentIndex,
                        int regexCurrentIndex)
{
    //Base
    if (strCurrentIndex > strEndIndex || regexCurrentIndex >= regex.Length)
    {
        for (int i = regexCurrentIndex; i < regex.Length; i++)
        {
            if (regex[i] != '*')
                return false;
        }
        return true;
    }

    //Induction
    if (regex[regexCurrentIndex] == '*')
    {
        for (int i = strCurrentIndex; i <= strEndIndex; i++)
        {
            if (HasMatch(str, regex, strEndIndex, i, regexCurrentIndex + 1))
                return true;
        }
        return false;
    }
    else if (regex[regexCurrentIndex] >= '0' && regex[regexCurrentIndex] <= '9')
    {
        int steps = (int)(regex[regexCurrentIndex] - '0');
        if (strCurrentIndex + steps > strEndIndex)
            return false;
        return HasMatch(str, regex, strEndIndex, strCurrentIndex + steps, regexCurrentIndex + 1);
    }
    else
    {
        if (str[strCurrentIndex] != regex[regexCurrentIndex])
            return false;
        return HasMatch(str, regex, strEndIndex, strCurrentIndex + 1, regexCurrentIndex + 1);
    }
}

Comments

Popular posts from this blog

Advent of Code - Day 6, 2024: BFS and FSM

Advent of Code - Day 7, 2024: Backtracking and Eval

Golang vs. C#: performance characteristics (simple case study)