您的位置:首页 > 其它

吊胃口 III (关键词:正则表达式,正则表达式构造器,保证精彩!)

2004-07-21 12:59 302 查看
不好意思啊各位,不是存心给大家卖关子的。因为我的确还没有完成,只是阶段性的成果,所以只能够一点一点给大家挤牙膏了。

我记得第一次最后给大家看了一个图片,左边那部分可以从大量的“单词”当中,找出符合前面已经输入的那一部分的那些单词。这个是一个自己写的类(从头到脚都是自己写的,除了书组和基本的类型,没有用其它的集合类型),在上一次已经初步完成了,但是今天不准备给大家放出来(还得继续吊着

),因为:1、还没有完成(测试);2、功能还有待调整。其实很明显,第二次吊胃口所放出来的代码,就是为了解决将搜索到的单词,以“智能提示”下拉框的方式显示出来并允许选择的问题。

那么这一切又一切的工作又是为了什么呢?呵呵,为了写一个超强的正则表达式构造器!嗯,大家一定记得上一次我给大家提供过一个alpha版的正则表达式构造器吧?虽然说挺好用的,但是里面有一些隐藏得我都不知道在哪里的Bug存在,还有一些结构不合理的地方(包括代码),以及还是不是非常顺手。所以我决定推倒重来,做一个全新的,非常好用的正则表达式构造器!

目前这个东西还处于非常核心的地方,没有任何可以看得见的效果存在,但是我可以给大家模拟一下主编辑区的效果,主要是给大家看一下我设计的语法:
/ Comment: This is a sample code acceptable by NfaGen2
/ Attn: This is only a SAMPLE, might not be acceptable in final release version.
/ Sample create: 2004-07-21
/ Creator: Sumtec

/ Option Syntax:
/ @OPTIONNAME OPTIONAME...
/
/ The following option means (?>...|...)
/ The opposite option should be: (which means (?:...|...) )
/ @option trace on
/ This option might not be supported
@option trace off

/ Define Syntax:
/ (>|:)?#?(\?|_)?NAME\.ALIAS:CONTEXT
/ >|:  means (?>CONTEXT) or (?:CONTEXT), it will ignore the @option trace.
/ #    means the definition here is a character set, or we say it's “[CONTEXT]”
/ ?|_ means it is a “captured replace” or “pure replace”,  or we say:
/ (?<NAME>CONTEXT) or CONTEXT
/ If (?|_) is omitted,  it will determine by @option capture which defaults to “caputure replace”
/ .ALIAS   is not a part of capture name in RegEx, but a part of definition name in NfaGen2
/ Some times we need to capture different context by the same capture groupname, .ALIAS provide
/ a way to get two or more different captures a same name.

/ Here gives you a full sample:
/ This sample will match the following text:
/ ABCD      0123  0123

_ws:\s
_EndString:\Z
#_word:a-zA-Z
#_number:\d
#Invalid.Word:^<_word>
#Invalid.Number:^<_number>
Word:(?><_word>|<Invalid.Word>)+
Number:(?><_number>|<Invalid.Number>)+
_Root:<Word>(?><_ws:many><Number@FirstNumber>(?><_ws:many><Number>)*)?

/ NfaGen2 will compile _Root into the following RegEx expression:
/ (?<Word>(?>[a-zA-Z]|(?<Invalid>[^a-zA-Z]))+)(?>\s+(?<FirstNumber>(?>\d|(?<Invalid>[^\d])))
/ (?>\s+(?<Number>(?>\d|(?<Invalid>[^\d])))*)?
/
/ I inserted line break for reading more easier.
/ There should not be any line breaks in the NfaGen2 generated expressions.
/ You might find optimization, for example: [\d] is optimized into \d
/ And you can see that <Number@FirstNumber> is redirected to (?<FirstNumber>...).
/ It is an example of the @ operator which can only be used in Context part.

呵呵!是不是比较酷呢?如果对格式有什么疑问或者建议,大家尽可以回复,谢谢支持!(看在我这么辛苦的进行手动着色,没有功劳也有苦劳的份上,多多支持哦!)

最后还是忍不住要说一下,上一次的Post里面提到我用那个alpha版的正则表达式构造器所产生的最长的表达式有七百多个字符,今天早上被我打破了,而且仅仅是一个上午的时间!这一次为了进行上面那个语法分析,目前已经有1,725个字符了,而且很可能要继续变长,因为目前对于CONTEXT部分还没有任何的分析。

我把这个正则表达式给大家,大家可以试一下匹配上面的Example,看看是否能够正确匹配:
(?>(?<Comment>/(?<CommentText>[^\n]*))|(?<Option>@(?<OptionName>(?>(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n]))|(?<OptionNameHead>[^0-9\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<OptionNameReject>[^>:\.@\r\n\s]))(?:[^\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<OptionNameReject>[^>:\.@\r\n\s]))*(?:(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n])))?))(?>[\f\t\v\x85\p{Z}]+(?<OptionName>(?>(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n]))|(?<OptionNameHead>[^0-9\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<OptionNameReject>[^>:\.@\r\n\s]))(?:[^\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<OptionNameReject>[^>:\.@\r\n\s]))*(?:(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n])))?))?)*[\f\t\v\x85\p{Z}]*)|(?<Define>(?<DefineGroupName>(?<TraceSelect>(?<ForceTrace>:)|(?<ForceNotTrace>>)race>)?(?<Char>#)?(?<GroupCapturePrefix>(?<ForceCapture>\?)|(?<ForceNotCapture>_))?(?<Groupname>(?>(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n]))|(?<NameHead>[^0-9\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<NameReject>[^>:\.@\r\n]))(?:[^\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<NameReject>[^>:\.@\r\n]))*(?:(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n])))?))(?:\.(?<Alias>(?>(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n]))|(?<NameHead>[^0-9\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<NameReject>[^>:\.@\r\n]))(?:[^\s\p{S}\p{Pe}\p{Pd}\p{Pf}\p{Pi}\p{Ps}\p{Po}]|(?<NameReject>[^>:\.@\r\n]))*(?:(?<![\f\t\v\x85\p{Z}])[>:\.@](?<InComplete>(?=[>:\.@\r\n])))?)))?(?<RejectDefineName>>[^:\r\n]*)?)(?>(?<DefineInComplete>(?<!\G)(?=[\f\t\v\x85\p{Z}]*[\r\n]|\Z))|:(?<DefineLine>[^\n]*)))|(?<RejectLine>[^\n]+)|)[\f\t\v\x85\p{Z}]*(?>[\r\n]|\Z)

注意:中间没有任何的空格和回车!最后也没有回车(这个千万注意)!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: