作者: blood
比较长,不过支持全部的关键字,直接就可以用了。
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>";
const string TAG_COMMENT = @"<font colr=#008200>";
const string TAG_ECOMMENT = @"</font>";
//
public CodeAnalysis()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 处理VB.NET代码,彩色化..
/// </summary>
/// <param name="Code">传入的Code</param>
/// <returns>处理过后的代码</returns>
public string ParseVB(string Code)
{
//
//定义VB.NET中关键字,将其存为数组
//
string[] VB_Keyword = new string[]
{
"AddHandler","AddressOf","AndAlso","Alias","And","Ansi","As","Assembly","Auto","Boolean",
"ByRef","Byte","ByVal","Call","Case","Catch","CBool","CByte","CChar",
"CDate","CDec","CDbl","Char","CInt","Class","CLng","CObj","Const",
"CShort","CSng","CStr","CType","Date","Decimal","Declare","Default",
"Delegate","Dim","DirectCast","Do","Double","Each","Else","ElseIf","End",
"Enum","Erase","Error","Event","Exit","False",
"Finally","For","Friend","Function","Get","GetType","GoTo","Handles","If",
"Implements","Imports","In","Inherits","Integer","Interface",
"Is","Let","Lib","Like","Long","Loop","Me","Mod","Module",
"MustInherit","MustOverride","MyBase","MyClass","Namespace","New","Next","Not","Nothing",
"NotInheritable","NotOverridable","Object","On","Option","Optional","Or","OrElse",
"Overloads","Overridable","Overrides","ParamArray","Preserve","Private","Property","Protected","Public",
"RaiseEvent","ReadOnly","ReDim","RemoveHandler","Resume","Return",
"Select","Set","Shadows","Shared","Short","Single","Static","Step","Stop",
"String","Structure","Sub","SyncLock","Then","Throw",
"To","True","Try","TypeOf","Unicode","Until","Variant","When","While",
"With","WithEvents","WriteOnly","Xor"
};
//
//设定转换代码颜色
//
string ReplaceVBComment = TAG_COMMENT + "$1" + TAG_ECOMMENT;
string ReplaceVBKeyword = TAG_FNTBLUE + "${char}" + TAG_EFONT;
//开始转换
for (int i=0;i<VB_Keyword.Length;i++)
{
string TempDirectives = @"(?<char>(\s" + VB_Keyword[i] + "|" + VB_Keyword[i] + @"\s))";
Code = Regex.Replace(Code,TempDirectives,ReplaceVBKeyword,RegexOptions.IgnoreCase);
Code = Regex.Replace(Code,@"'(?<x>[^\r\n]*)",ReplaceVBComment);
Code = Regex.Replace(Code,@"REM (?<x>[^\r\n]*)",ReplaceVBComment);
}
return Code;
}
}
}
-
vb.net教程之完整的VB.NET的语法解析程序
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比