返回列表 发帖

[求助]匹配成对嵌套的if ... end if 代码段

使用语言:JS,VBS
范例文本:
  1. if isnumeric(text1) then
  2.     if int(val(text1))=val(text1) then
  3.         msgbox "Text1中是整数"
  4.     else
  5.         msgbox "Text1中非整数"
  6.     end if   
  7. else
  8.     msgbox "Text1中非数字"
  9. end if
复制代码
目标:将所有的IF代码段匹配出来。

注:其实这里需要匹配的就是类似一下的成对IF语句
if ...then
if ...then
else
end if
end if

问题解决。回来贴。先吃饭去。
在答疑解惑版提问时,请注明所用语言、范例文本、匹配结果。谢谢!
------------------------------------------------------------------------------
我爱正则表达式
GTalk: rex[at]zhasm[dot]com
Twitter: rex_zhasm

TOP

中间应该还有else if ...then 吧,作为分支结构。

这种情况应该使用支持recursive的正则表达式了。
我想到 ...
rex 发表于 2010-8-10 11:10



     有些有Else分支,有些是没有的。

TOP

原理:
if结构的完整形式应该是这样的:
  1. if condition then (must)
  2. else if condition then (optional, can be multiple)
  3. else (optional)
  4. end if (must)
复制代码
其中,只有if condition then 和end if 是一定出现的。而else if condition then可能出现0 次或多次(可以用*修饰),不带条件的else只能出现0 次或一次(可以用?来修饰)。

以下是javascript代码:
  1. if .*? then[\s\r\n]+(?:.|[\s\r\n])+?(?:else if .*? then[\s\r\n]+(?:.|[\s\r\n])+?)*else[\s\r\n]+(?:.|[\s\r\n])+?end if
复制代码
贴图在此:
附件: 您需要登录才可以下载或查看附件。没有帐号?注冊
在答疑解惑版提问时,请注明所用语言、范例文本、匹配结果。谢谢!
------------------------------------------------------------------------------
我爱正则表达式
GTalk: rex[at]zhasm[dot]com
Twitter: rex_zhasm

TOP

原理:
if结构的完整形式应该是这样的:其中,只有if condition then 和end if 是一定出现的。而else if co ...
rex 发表于 2010-8-10 12:37



    给出的示例文本,这种情况就无法匹配全。还有些是if ... then ... end if 直接写在一行上的,像这样
  1. if isnumeric(text1) then msgbox "Text1中是数字"
  2. end if
复制代码
也无法匹配到

TOP

这段代码可以匹配其中不嵌套的if 段。不过,对于嵌套的if,它还不行。使用javascript的正则,有些弱。我看看如何实现。(或许换一种语言)。代码见下:
  1. <html>
  2.     <head>
  3.         <meta http-equiv="content-type" content="text/html; charset=utf-8"0>
  4.    
  5.         <title>Abc</title>
  6.         <script type="text/javascript" charset="utf-8">
  7.             var str="if isnumeric(text1) then \n"+
  8.                 "msgbox \"Text1中是数字\"\n"+
  9.                 "end if\n"+
  10.                 "if isnumeric(text1) then"+
  11.                 "    if int(val(text1))=val(text1) then\n"+
  12.                 "        msgbox \"Text1中是整数\"\n"+
  13.                 "    else\n"+
  14.                 "        msgbox \"Text1中非整数\"\n"+
  15.                 "    end if   \n"+
  16.                 "else\n"+
  17.                 "    msgbox \"Text1中非数字\"\n"+
  18.                 "end if \n"
  19.             var re = /if\s.*?then[\s\S]+?(?:else\s*if.*?then[\s\S]+?)*(?:else(?!\s*if)[\s\S]+?)?end\s*if/ig;
  20.             var match = re.exec(str);
  21.             while (match != null) {
  22.                 // matched text: match[0]
  23.                 // match start: match.index
  24.                 // capturing group n: match[n]
  25.                 alert(match[0]);
  26.                 match = re.exec(str);
  27.             }
  28.         </script>
  29.     </head>
  30. </html>
复制代码
在答疑解惑版提问时,请注明所用语言、范例文本、匹配结果。谢谢!
------------------------------------------------------------------------------
我爱正则表达式
GTalk: rex[at]zhasm[dot]com
Twitter: rex_zhasm

TOP

这段代码可以匹配其中不嵌套的if 段。不过,对于嵌套的if,它还不行。使用javascript的正则,有些弱。我看 ...
rex 发表于 2010-8-10 13:18



   嗯,对于嵌套的if 确实有点不好办。

TOP

使用perl的recursive regex解决。代码如下。
  1. #!/usr/bin/perl
  2. #author:         rex
  3. #blog:           http://iregex.org
  4. #filename        if.pl
  5. #created:        2010-08-10
  6. $str=<<END;
  7. hello world
  8. if isnumeric(text1) then
  9.     if int(val(text1))=val(text1) then
  10.         msgbox "Text1中是整数"
  11.     else
  12.         msgbox "Text1中非整数"
  13.     end if   
  14. else
  15.     msgbox "Text1中非数字"
  16. end if  
  17. pig world
  18. END

  19. $seg=qr{
  20.    (

  21.         ^\s*if.*$ [\r\n]*
  22.         (?:
  23.             ^(?!\s*(?:(?:end\s*)?if)).*$ [\r\n]* |
  24.             (??{$seg})
  25.         )*
  26.         ^\s*end\s+if.*$ [\r\n]*?
  27.     )
  28.     }imx;

  29. if ($str =~ $seg)
  30. {
  31.     print $1,"\n";
  32. }
复制代码
在我的机器上(Ubuntu10.04+Perl 5.10),输出是:
  1. if isnumeric(text1) then
  2.     if int(val(text1))=val(text1) then
  3.         msgbox "Text1中是整数"
  4.     else
  5.         msgbox "Text1中非整数"
  6.     end if   
  7. else
  8.     msgbox "Text1中非数字"
  9. end if  
复制代码
在答疑解惑版提问时,请注明所用语言、范例文本、匹配结果。谢谢!
------------------------------------------------------------------------------
我爱正则表达式
GTalk: rex[at]zhasm[dot]com
Twitter: rex_zhasm

TOP

回复 8# rex


    才看到老大的回复,能讲下中间是如何递归的吗?

TOP

在答疑解惑版提问时,请注明所用语言、范例文本、匹配结果。谢谢!
------------------------------------------------------------------------------
我爱正则表达式
GTalk: rex[at]zhasm[dot]com
Twitter: rex_zhasm

TOP

返回列表