Exemplo n.º 1
0
        protected virtual bool HandleMatch(Match match, Token token, StringBuilder sb, int index, int count, ref int offset)
        {
            Group group;

            if (this.GroupName != null && this.GroupName.Length > 0)
            {
                group = match.Groups[this.GroupName];
            }
            else
            {
                group = match.Groups[this.GroupIndex];
            }

            PseudoMatch pseudoMatch = this.IsPseudoMatch(group, token, index, count);

            if (!pseudoMatch.Match)
            {
                return(pseudoMatch.CancelSearch);
            }

            int pos;

            switch (this.Position)
            {
            case SelectorPosition.After:
                pos = group.Index + group.Length;
                break;

            case SelectorPosition.Before:
                pos = group.Index;
                break;

            case SelectorPosition.Replace:
                pos = group.Index;
                sb.Remove(pos + offset, group.Length);
                break;

            default:
                throw new IndexOutOfRangeException();
            }

            sb.Insert(pos + offset, token.Output);

            if (this.Position == SelectorPosition.Replace)
            {
                offset -= group.Length;
            }

            offset += token.Output != null ? token.Output.Length : 0;

            return(pseudoMatch.CancelSearch);
        }
Exemplo n.º 2
0
        public virtual List <string> Matches(string text, Token token)
        {
            List <string>   list    = new List <string>();
            MatchCollection matches = this.Regex.Matches(text);
            int             count   = matches.Count;

            for (int i = 0; i < count; i++)
            {
                Group group;

                if (this.GroupName != null && this.GroupName.Length > 0)
                {
                    group = matches[i].Groups[this.GroupName];
                }
                else
                {
                    group = matches[i].Groups[this.GroupIndex];
                }

                if (this.Pseudo == null || this.Pseudo.Length == 0)
                {
                    list.Add(group.Value);
                    break;
                }

                PseudoMatch pseudoMatch = this.IsPseudoMatch(group, token, i, count);

                if (pseudoMatch.Match)
                {
                    list.Add(group.Value);
                }

                if (pseudoMatch.CancelSearch)
                {
                    break;
                }
            }

            return(list);
        }