比較長
不過支持全部的關鍵字
直接就可以用了
using System;
using System
Text;
using System
Text
RegularExpressions;
namespace ComOSLeagueComponent
{
/// <summary>
/// 語法分析器將所有Code根據語法進行變色
/// <list type=VB>支持VBNET</list>
/// <list type=CS>支持CS</list>
/// <author>掉掉</author>
/// <date>年月日</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=backgroundcolor: yellow;>;
const string TAG_ESPAN = @</span>;
const string TAG_B = @<b>;
const string TAG_EB = @</b>;
const string TAG_COMMENT = @<font colr=#>;
const string TAG_ECOMMENT = @</font>;
//
public CodeAnalysis()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
/// <summary>
/// 處理VBNET代碼彩色化
/// </summary>
/// <param name=Code>傳入的Code</param>
/// <returns>處理過後的代碼</returns>
public string ParseVB(string Code)
{
//
//定義VBNET中關鍵字將其存為數組
//
string[] VB_Keyword = new string[]
{
AddHandlerAddressOfAndAlsoAliasAndAnsiAsAssemblyAutoBoolean
ByRefByteByValCallCaseCatchCBoolCByteCChar
CDateCDecCDblCharCIntClassCLngCObjConst
CShortCSngCStrCTypeDateDecimalDeclareDefault
DelegateDimDirectCastDoDoubleEachElseElseIfEnd
EnumEraseErrorEventExitFalse
FinallyForFriendFunctionGetGetTypeGoToHandlesIf
ImplementsImportsInInheritsIntegerInterface
IsLetLibLikeLongLoopMeModModule
MustInheritMustOverrideMyBaseMyClassNamespaceNewNextNotNothing
NotInheritableNotOverridableObjectOnOptionOptionalOrOrElse
OverloadsOverridableOverridesParamArrayPreservePrivatePropertyProtectedPublic
RaiseEventReadOnlyReDimRemoveHandlerResumeReturn
SelectSetShadowsSharedShortSingleStaticStepStop
StringStructureSubSyncLockThenThrow
ToTrueTryTypeOfUnicodeUntilVariantWhenWhile
WithWithEventsWriteOnlyXor
};
//
//設定轉換代碼顏色
//
string ReplaceVBComment = TAG_COMMENT + $ + TAG_ECOMMENT;
string ReplaceVBKeyword = TAG_FNTBLUE + ${char} + TAG_EFONT;
//開始轉換
for (int i=;i<VB_KeywordLength;i++)
{
string TempDirectives = @(?<char>(\s + VB_Keyword[i] + | + VB_Keyword[i] + @\s));
Code = RegexReplace(CodeTempDirectivesReplaceVBKeywordRegexOptionsIgnoreCase);
Code = RegexReplace(Code@(?<x>[^\r\n]*)ReplaceVBComment);
Code = RegexReplace(Code@REM (?<x>[^\r\n]*)ReplaceVBComment);
}
return Code;
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/14615.html