正则表达式匹配嵌套标示符{},(),html tag等

来源:互联网 发布:linux efi boot 编辑:程序博客网 时间:2024/05/16 06:49

昨儿个终于在<m...regex>看到最有意思的部分了,正则表达式匹配嵌套标示符,这样的话就在.cpp源文件里头查找函数(()),类({}),在html, xml(<X>)文件里面查找任意的值,很简单就能写个Parser了,如<b><b><b>a</b></b></b>的匹配完全没问题。以前在只会C/C++的时候这可是最难的题目之一,下面这个脚本用来查找.cpp文件中的类,把{}改为<X>就能找html的tag了。

perltidy


#this script by Zui Tao is to find the class name not being nested in other classesundef $/;$text = <>;my $levelN;$levelN = qr{ (?>[^{}]+|/{ (??{ $levelN })   /} )* }xs;while ( $text =~ m/ /b class /s* ( /w+ ) /s* /{ $levelN /} /xg ) {    print "/nclass name is $1";}
原创粉丝点击