Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / C#

Regular Expressions Syntax Highlighting

Rate me:
Please Sign up or sign in to vote.
4.34/5 (17 votes)
7 Sep 2008CPOL3 min read 40.5K   1K   43  
Easy to use syntax highlighting class
using System.Collections.Generic;

namespace tevton.SyntaxHighlight {
    public class SyntaxHighlightSegmentList: List<SyntaxHighlightSegment> {
        /// <summary>
        /// Remove segments from this list that are overlapped by those in masterList
        /// </summary>
        /// <param name="masterList"></param>
        public virtual void RemoveOverlappingSegments(SyntaxHighlightSegmentList masterList) {
            SyntaxHighlightSegmentList res = new SyntaxHighlightSegmentList();
            foreach(SyntaxHighlightSegment thisSegment in this) {
                bool doAdd = true;
                foreach(SyntaxHighlightSegment masterSegment in masterList) {
                    if(masterSegment.SuperiorTo(thisSegment)) {
                        doAdd = false;
                        break;
                    }
                }
                if(doAdd) {
                    res.Add(thisSegment);
                }
            }
            this.Clear();
            this.AddRange(res);
        }
        /// <summary>
        /// Remove segments from this list that are overlapped by another segments in the same list
        /// </summary>
        public virtual void RemoveOverlappingSegments() {
            SyntaxHighlightSegmentList res = new SyntaxHighlightSegmentList();
            res.AddRange(this);
            for(int i = res.Count - 1; i >= 0; i--) {
                for(int j = 0; j < i; j++) {
                    if(this[j].SuperiorTo(res[i])) {
                        res.RemoveAt(i);
                        break;
                    }
                }
            }
            this.Clear();
            this.AddRange(res);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions