SUMTEC -- There's a thing in my bloglet.

But it's not only one. It's many. It's the same as other things but it exactly likes nothing else...

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  263 随笔 :: 19 文章 :: 3009 评论 :: 74万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

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

注意:中间没有任何的空格和回车!最后也没有回车(这个千万注意)!

posted on   Sumtec  阅读(2823)  评论(15编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示