当前位置:编程学习 > C#/ASP.NET >>

穿针引线:一个不完全的对代码加亮的代码,大家可以对其进行扩展!(原创)

答案:using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Com.OSLeague.Component
{
    /// <summary>
    /// 语法分析器,将所有Code根据语法进行变色
    /// <list type="VB">支持VB.NET</list>
    /// <list type="CS">支持CS</list>
    /// <author>掉掉</author>
    /// <date>2002年5月14日</date>
    /// <Memo>
    /// 练习正则表达式
    /// </Memo>
    /// </summary>
    public class CodeAnalysis
    {

        //
        //定义HTML开始和结束的语句,用于语法变色
        //

        const string TAG_FNTRED    = @"<font color=""red"">";
        const string TAG_FNTBLUE   = @"<font color=""blue"">" ;
        const string TAG_FNTGRN    = @"<font color=""green"">" ;
        const string TAG_FNTMRN    = @"<font color=""maroon"">" ;
        const string TAG_FNTBLACK  = @"<font color=""black"">" ;
        const string TAG_EFONT     = @"</font>" ;
        const string TAG_SPNYELLOW = @"<span style=""background-color: yellow;"">";
        const string TAG_ESPAN     = @"</span>";
        const string TAG_B         = @"<b>";
        const string TAG_EB        = @"</b>";

        //



        public CodeAnalysis()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

    
        
        /// <summary>
        /// 处理CS代码,彩色化..
        /// </summary>
        /// <param name="Code">传入的Code</param>
        /// <returns>处理过后的代码</returns>
        public string ParseCS(string Code)
        {
            //
            //定义CS中关键字,将其存为数组
            //

            string[] Cs_Keyword = new string[]
            {
                "as", "auto","base","break","case","catch","const","continue","default","do",
                "else","event","explicit","extern","false","finally","fixed","for","foreach",
                "goto","if,","implicit","in","internal","lock","namespace","null","operator",
                "out","override","params","private","protected","public","readonly","ref",
                "return","sealed","stackalloc","static","switch","this","throw","true","try",
                "unsafe","using","virtual","void","while","new"
            };

            

            //
            //设定转换代码颜色
            //

            string ReplaceCsKeyword = TAG_FNTBLUE + "${char}" + TAG_EFONT;
            //开始转换
            for (int i=0;i<Cs_Keyword.Length;i++)
            {
                string TempDirectives = @"(?<char>(\s" + Cs_Keyword[i] + "|" + Cs_Keyword[i] + @"\s))";
                Code = Regex.Replace(Code,TempDirectives,ReplaceCsKeyword);
            }

            
            //
            //定义CS中数据类型,将其存为数组
            //

            string[] Cs_DataType = new string[]
            {
            "bool","byte","char","class","decimal","delegate","double","enum","float",
            "int","interface","long","object","sbyte","short","string","struct&

上一个:Working with Anchoring and Docking Properties(ZT)
下一个:Window Hiding with C#(ZT)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,