比较长
不过支持全部的关键字
直接就可以用了
using System;
using SystemText;
using SystemTextRegularExpressions;
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;
}
}
}