System.Text.RegularExpressions.Match.BalanceMatch C# (CSharp) Method

BalanceMatch() private method

private BalanceMatch ( int cap ) : void
cap int
return void
        internal virtual void BalanceMatch(int cap)
        {
            int capcount;
            int target;

            _balancing = true;

            // we'll look at the last capture first
            capcount = _matchcount[cap];
            target = capcount * 2 - 2;

            // first see if it is negative, and therefore is a reference to the next available
            // capture group for balancing.  If it is, we'll reset target to point to that capture.
            if (_matches[cap][target] < 0)
                target = -3 - _matches[cap][target];

            // move back to the previous capture
            target -= 2;

            // if the previous capture is a reference, just copy that reference to the end.  Otherwise, point to it.
            if (target >= 0 && _matches[cap][target] < 0)
                AddMatch(cap, _matches[cap][target], _matches[cap][target + 1]);
            else
                AddMatch(cap, -3 - target, -4 - target /* == -3 - (target + 1) */ );
        }

Usage Example

Example #1
0
        /*
         * Called by Go() to capture a subexpression. Note that the
         * capnum used here has already been mapped to a non-sparse
         * index (by the code generator RegexWriter).
         */
        protected void TransferCapture(int capnum, int uncapnum, int start, int end)
        {
            int start2;
            int end2;

            // these are the two intervals that are cancelling each other

            if (end < start)
            {
                int T;

                T     = end;
                end   = start;
                start = T;
            }

            start2 = MatchIndex(uncapnum);
            end2   = start2 + MatchLength(uncapnum);

            // The new capture gets the innermost defined interval

            if (start >= end2)
            {
                end   = start;
                start = end2;
            }
            else if (end <= start2)
            {
                start = start2;
            }
            else
            {
                if (end > end2)
                {
                    end = end2;
                }
                if (start2 > start)
                {
                    start = start2;
                }
            }

            Crawl(uncapnum);
            _runmatch.BalanceMatch(uncapnum);

            if (capnum != -1)
            {
                Crawl(capnum);
                _runmatch.AddMatch(capnum, start, end - start);
            }
        }
All Usage Examples Of System.Text.RegularExpressions.Match::BalanceMatch