예제 #1
0
        /// <summary>
        /// Either returns a weakly cached RegexReplacement helper or creates one and caches it.
        /// </summary>
        /// <returns></returns>
        public static RegexReplacement GetOrCreate(WeakReference <RegexReplacement> replRef, string replacement, Dictionary <int, int> caps,
                                                   int capsize, Dictionary <string, int> capnames, RegexOptions roptions)
        {
            RegexReplacement repl;

            if (!replRef.TryGetTarget(out repl) || !repl.Pattern.Equals(replacement))
            {
                repl = RegexParser.ParseReplacement(replacement, roptions, caps, capsize, capnames);
                replRef.SetTarget(repl);
            }

            return(repl);
        }
예제 #2
0
        /// <summary>
        /// Replaces all occurrences of the previously defined pattern with the
        /// <paramref name="replacement"/> pattern, starting at the character position
        /// <paramref name="startat"/>.
        /// </summary>
        public string Replace(string input, string replacement, int count, int startat, ref long replacements)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (replacement == null)
            {
                throw new ArgumentNullException(nameof(replacement));
            }

            // a little code to grab a cached parsed replacement object
            RegexReplacement repl = (RegexReplacement)_replref.Get();

            if (repl == null || !repl.Pattern.Equals(replacement))
            {
                repl = RegexParser.ParseReplacement(replacement, caps, capsize, capnames, roptions);
                _replref.Cache(repl);
            }

            return(repl.Replace(this, input, count, startat, ref replacements));
        }
예제 #3
0
        /// <summary>
        /// Returns the expansion of the passed replacement pattern. For
        /// example, if the replacement pattern is ?$1$2?, Result returns the concatenation
        /// of Group(1).ToString() and Group(2).ToString().
        /// </summary>
        public virtual string Result(string replacement)
        {
            RegexReplacement repl;

            if (replacement == null)
            {
                throw new ArgumentNullException(nameof(replacement));
            }

            if (_regex == null)
            {
                throw new NotSupportedException(SR.NoResultOnFailed);
            }

            repl = (RegexReplacement)_regex._replref.Get();

            if (repl == null || !repl.Pattern.Equals(replacement))
            {
                repl = RegexParser.ParseReplacement(replacement, _regex.caps, _regex.capsize, _regex.capnames, _regex.roptions);
                _regex._replref.Cache(repl);
            }

            return(repl.Replacement(this));
        }