Exemplo n.º 1
0
        /// <summary>
        /// Applies change in regular expression format to input string.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="options"></param>
        /// <param name="exceptions"></param>
        /// <returns></returns>
        private string ApplyRegex(string input, ChangeOptions options, IList <Change> exceptions)
        {
            if (!IsReplacementApplicable())
            {
                return(input);                                        //nedochází k žádné změně
            }
            //pravidla počtu nahrazení pro Regex: -1 = všechny výskyty; 0 = žádný výskyt; 1 a více = zadaný počet výskytů
            //pravdila Cahnge: null, 0 = žádný výskyt

            string output           = input;
            int    replacementCount = GetReplacementCount(options);

            Occurrences exceptionMatches = ExceptionMatches(input, exceptions);

            if (exceptionMatches.Count == 0)
            {
                return(base.Regex.Replace(input, Replace, replacementCount));
            }


            MatchCollection patternMatches = base.Regex.Matches(input);
            int             count          = 0;

            output = input;
            foreach (Match match in patternMatches)
            {
                Occurrence occurrence = new Occurrence(match.Index, match.Index + match.Length);
                if (!exceptionMatches.IsOccurrenceWithin(occurrence))
                {
                    output = base.Regex.Replace(output, Replace, 1, match.Index);
                    count++;
                }
                if (count == options.ReplacementCount)
                {
                    return(output);
                }
            }

            return(output);
        }
Exemplo n.º 2
0
        private string Output(ChangeOptions options, int count, Occurrences exceptionMatches, string output)
        {
            int start = 0;

            for (int i = 0; i < count; i++)
            {
                int        loc        = output.IndexOf(Pattern, start, StringComparison.Ordinal);
                Occurrence occurrence = new Occurrence(loc, loc + Pattern.Length);

                while (exceptionMatches.IsOccurrenceWithin(occurrence))
                {
                    loc        = output.IndexOf(Pattern, loc + 1, StringComparison.Ordinal);
                    occurrence = new Occurrence(loc, loc + Pattern.Length);
                }
                if (loc == -1)
                {
                    return(output);
                }

                output = ReplaceFirstOccurrence(output, Pattern, Replace, options, loc);
                start  = loc + 1;
            }
            return(output);
        }