思归呓语

衣带渐宽终不悔,为伊消得人憔悴
随笔 - 413, 评论 - 2971, 引用 - 245

导航

关于

标签

每月存档

最新留言

广告

不太好理解的Alternation Constructs

看到有人对.NET中正则表示式的Alternation Constructs不太理解,这里写个简单例子说明一下。

英语里说 "0 feet"(0英尺), "1 foot"(一英尺),"2 feet"(二英尺),"3 feet"(三英尺),...我们怎么来匹配这些字符串?

这个例子很简单,象 
 "^0*1 foot|[02-9] feet|[1-9]\d+ feet$"

 "^0*1 foot|(?:(?:[02-9]|[1-9]\d+) feet)$"

这样的表达式大概就行了。

下面用Alternation Constructs来演示一下。

1。 "^\d+ f(?(?<=\b0*1 f)oo|ee)t$"

在这里,(?<=\b0*1 f) is a zero-width positive lookbehind, (?(?<=\b0*1 f)oo|ee) 表示

假如前面3个字符是1 f的话(有可能有些前置的0),那么匹配oo,否则匹配ee

2。 "^((?<one>0*1)|[02-9]|\d{2,}) f(?(one)oo|ee)t$"

在前面我们尝试匹配1(有可能有些前置的0),假如1被匹配的话,表明第一部分(?<one>0*1)被captured了,group "one"就有了定义

在第二部分里

(?(one)oo|ee)

说,假如前面的group "one"被captured了,那么就匹配oo,否则匹配ee

这里是个测试编码

using System;
using System.Text.RegularExpressions;

class TestRegAC
{
  static void Main()
  {
 string[] slist = {"11 foot", "01 feet", "1 foot", "1 feet", "2 feet", "3 foot", "10 feet", "100 foot", "0 feet", "001 foot"};
 Regex re = new Regex(@"^\d+ f(?(?<=\b0*1 f)oo|ee)t$", RegexOptions.IgnoreCase);
 foreach (string s in slist)
 {
  Console.WriteLine("{0} matches? {1}", s, re.IsMatch(s));
 }

 Console.WriteLine();

 re = new Regex(@"^((?<one>0*1)|[02-9]|\d{2,}) f(?(one)oo|ee)t$", RegexOptions.IgnoreCase);
 foreach (string s in slist)
 {
  Console.WriteLine("{0} matches? {1}", s, re.IsMatch(s));
 }
  }
}

同时参考

Manipulate Text With Regular Expressions

里面有个匹配25-09-2003后任一天的例子,很有意思

posted on 2004-08-13 05:19:00 by saucer  评论(7) 阅读(4287)

Gates的业余爱好

最近,Bill Gates邀请微软新实习生到他家野餐,这好像是一年一度的事情。在网上读到了Jeff Maurone详述的这次经历(12),提到豪华的房子, Gates的平易近人,与女儿在一起的场面

猜猜他的业余爱好是什么?Medicine 和 bio-tech!

这里是一段Bill Gates的话


"I'm a technologist. My brain is wired to think about software and how it should work. It's kind of my one shot. I'm not a businessman and most times I don't even know what it means to be a businessman. If you've got this one thing you do that is your passion, that's really the only thing at which you have a shot of being world class."

posted on 2004-08-05 10:38:00 by saucer  评论(14) 阅读(3730)

ASP.NET 2.0中的自定义表示式

在手头的项目里,不少动态网页里需要包含静态的XML文件,内含格式化的内容。用XML文件的原因是,内容/设计组的人员好编辑。我们是在编码里加了PlaceHolder或LiteralControl,读取这些文件,也许会做些过滤,然后把内容输出到网页里去。也可以做成Server Control,但因为涉及别的因素,没有这么做。

正好读到Jeff Prosise的blog里提到ASP.NET 2.0中的自定义表示式,感觉很有意思。ASP.NET QuickStart Tutorial的新版里提到了“Writing Expression Handlers”,但没编码可参考。在GOOGLE上查了一下,Marco Bellinaso通过Reflector琢磨出了怎么写Custom Expression Handlers,而且写了个跟我们的目的类似的Handler。但很可惜,我们现在还不能用ASP.NET 2.0

posted on 2004-08-04 01:01:00 by saucer  评论(6) 阅读(2318)

Jeffrey Richter is blogging

Jeffrey Richter和他的Wintellect公司的伙伴(包括Jeff Prosise, John Robbins )的blog

posted on 2004-08-04 00:35:00 by saucer  评论(3) 阅读(2466)

XPath+XSLT

看到了LostInet的Extension Object帖子,正好最近在写跟XPath有关的东西,从XML MVP Daniel Cazzulino处学到了不少东西,顺便看到MSDN上一些非常有用的信息,了解到可以通过XsltContext / IXsltContextFunction / IXsltContextVariable来实现设置自己的函数与参数。KB里有篇综述性文章:

INFO: Roadmap for Executing XSLT Transformations in .NET Applications

还有一篇提到XSLT转换性能的文章:

INFO: Performance of XSLT Transformations in the .NET Framework

里面提到了一些问题

1.用XmlDataDocument做转换慢,应该用XPathDocument
2.用到preceding-sibling时,通过XmlDocument转换大文件时会causes 100 percent CPU utilization
3.用xsl:key时转换会很慢
4.用Inline Script Blocks时,生成的Managed Assemblies不能正确地释放

posted on 2004-08-02 12:00:00 by saucer  评论(3) 阅读(3377)

Powered by: Joycode.MVC引擎 0.5.2.0