正则表达式的用途十分的广泛,常用在搜索和替换,几乎所有的编程语言都对其支持各种的地方,如js,java ,c#都有自己的实现类,所以在使用起来也是十分的方便.
很想弄明白他在每种语言中是如何做的,于是在没有弄得源码前,先动手用C#语言去做了一个支持正则表达式实现的类,目前实现功能十分的简单,支持IsMatch和Relace的处理基本字符的实现,
代码有诸多的不足,希望多多见谅.
示例代码:
lxfRegex lxf = new lxfRegex(txtregex.Text);


StringBuilder sb = new StringBuilder();
sb.AppendLine("括号结果");

foreach (MatchUnit mu in lxf.Matches(txtSrc.Text))

{
sb.AppendLine("Match匹配" + ind++);
int i = 0;
for (int j = 0; j < mu.Groups.Count; j++)

{
sb.AppendLine("Group" + j + mu.Groups[j].Value);

foreach (CaptureUnit t in mu.Groups[j].Captures)

{
sb.Append(" Capture" + i);
sb.Append(" Index:" + t.Index + " Length:" + t.Length);
sb.Append(" = " + t.Value);
sb.AppendLine();
i++;
}
}
}
this.txtMatch.Text = sb.ToString();
演示位置:
http://www.iyuyan.com/regex爱语言
下载源码:
源代码