Repository: microsoft/ApplicationInspector
File: AppInspector.RulesEngine/OatExtensions/OatRegexWithIndexOperation.cs (C#)

Code before:
        if (clause.DictData?.Count > 0)
        {
            yield return new Violation(
                string.Format(Strings.Get("Err_ClauseDictDataUnexpected"), rule.Name,
                    clause.Label ?? rule.Clauses.IndexOf(clause).ToString(CultureInfo.InvariantCulture),
                    clause.Operation.ToString()), rule, clause);
        }
    }

    /// <summary>
    ///     Returns results with pattern index and Boundary as a tuple to enable retrieval of Rule pattern level meta-data like
    ///     Confidence and report the
    ///     pattern that was responsible for the match
    /// </summary>
    /// <param name="clause"></param>
    /// <param name="state1"></param>
    /// <param name="state2"></param>
    /// <param name="captures"></param>
    /// <returns></returns>
    private OperationResult RegexWithIndexOperationDelegate(Clause clause, object? state1, object? state2,
        IEnumerable<ClauseCapture>? captures)
    {
        if (state1 is TextContainer tc && clause is OatRegexWithIndexClause src &&
            clause.Data is List<string> RegexList && RegexList.Count > 0)
        {
            RegexOptions regexOpts = Common.Utils.RegexModifierToRegexOptions(src.Arguments, enableNonbacktrackingRegex: src.Arguments.Contains("nb"));
            var subBoundary = state2 is Boundary s2 ? s2 : null;

            List<(int, Boundary)> outmatches = new(); //tuple results i.e. pattern index and where

            if (Analyzer != null)
            {
                foreach (var regexString in RegexList)
                    if (StringToRegex(regexString, regexOpts) is { } regex)
                    {
                        if (src.XPaths is not null)
                        {
                            foreach (var xmlPath in src.XPaths)
                            {
                                var targets = tc.GetStringFromXPath(xmlPath);
                                foreach (var target in targets)
                                {
                                    var matches = GetMatches(regex, tc, clause, src.Scopes, target.Item2);
                                    outmatches.AddRange(matches);
                                }
                            }
                        }

                        if (src.JsonPaths is not null)
                        {
                            foreach (var jsonPath in src.JsonPaths)

Review comment (refactor):
I think this is not necessary, since `nb` is added to the arguments when the pattern is added to the abstract rule set - as long as the `nb` and `b` cases are added to `RegexModifierToRegexOptions`. See also comment on `AbstractRuleSet`.

Code after:

        if (clause.DictData?.Count > 0)
        {
            yield return new Violation(
                string.Format(Strings.Get("Err_ClauseDictDataUnexpected"), rule.Name,
                    clause.Label ?? rule.Clauses.IndexOf(clause).ToString(CultureInfo.InvariantCulture),
                    clause.Operation.ToString()), rule, clause);
        }
    }

    /// <summary>
    ///     Returns results with pattern index and Boundary as a tuple to enable retrieval of Rule pattern level meta-data like
    ///     Confidence and report the
    ///     pattern that was responsible for the match
    /// </summary>
    /// <param name="clause"></param>
    /// <param name="state1"></param>
    /// <param name="state2"></param>
    /// <param name="captures"></param>
    /// <returns></returns>
    private OperationResult RegexWithIndexOperationDelegate(Clause clause, object? state1, object? state2,
        IEnumerable<ClauseCapture>? captures)
    {
        if (state1 is TextContainer tc && clause is OatRegexWithIndexClause src &&
            clause.Data is List<string> RegexList && RegexList.Count > 0)
        {
            RegexOptions regexOpts = Common.Utils.RegexModifierToRegexOptions(src.Arguments);
            var subBoundary = state2 is Boundary s2 ? s2 : null;

            List<(int, Boundary)> outmatches = new(); //tuple results i.e. pattern index and where

            if (Analyzer != null)
            {
                foreach (var regexString in RegexList)
                {
                    if (StringToRegex(regexString, regexOpts) is { } regex)
                    {
                        if (src.XPaths is not null)
                        {
                            foreach (var xmlPath in src.XPaths)
                            {
                                var targets = tc.GetStringFromXPath(xmlPath);
                                foreach (var target in targets)
                                {
                                    var matches = GetMatches(regex, tc, clause, src.Scopes, target.Item2);
                                    outmatches.AddRange(matches);
                                }
                            }
                        }

                        if (src.JsonPaths is not null)