Nuva 示例代码(每日一帖)之 正则测试器

<..========================================================
==                                                       ==
==                Macrobject Nuva Samples                ==
==                                                       ==
==      Copyright (c) 2004-2006 Macrobject Software      ==
==                                                       ==
==                  ALL RIGHTS RESERVED                  ==
==                                                       ==
==               http://www.macrobject.com               ==
==                                                       ==
========================================================..>
<.
  class MainForm = System.UI.HtmlForm()
    Caption = '正则测试器'
    Width   = 800
    Height  = 600

    var Pattern = 'function \w+'
    var Content = System.File.Load('正则测试器.nuvaexec')
    var Outtext

    function CreateBody().>
<table width="100%" height="100%">
  <tr height="5%" align="right">
    <td/><td><input type="button" value="Match All" mo:onclick="Match"></td>
  </tr>
  [.CreateTextarea('Pattern', 15, this.@Pattern).]
  [.CreateTextarea('Content', 35, this.@Content).]
  [.CreateTextarea('Outtext', 45, this.@Outtext).]
</table>

    <.function CreateTextarea(theCaption, theHeight, theBind).>
  <tr height="[.=theHeight.]%">
    <td width="20">[.=theCaption.]:</td>
    <td>
      <textarea style="width:100%;height:100%" mo:binddata="[.=theBind.]" mo:bindattr="value"></textarea>
    </td>
  </tr>
    <.end function
    end function

    function Match()
      this.SaveBindData()
      Outtext = Content.RegexMatch(Pattern, Function = 'DoMatch', Option = '')
      this.LoadBindData()

      function DoMatch(Index, Length, Value, Match)
        Result := Value ~ \r\n ~ '--------------------------------' ~ \r\n;
      end function
    end function

    function OnShow()
      this.LoadBindData()
      this.Match()
    end function
  end class

  var main = MainForm()
  main.Show()

  System.App.Run()
.>

<..
【简介】
    本例是一简单的正则测试程序,根据输入的正则串和相应的正文,点击 Match All 按钮,能够输出所有匹配串。

【看点】
    1、本例演示了 Nuva 语言的 GUI 应用、面向对象编程、事件驱动以及数据绑定,相应的内容请参考简单浏览器的示例程序及其说明。

    2、本例中编写了较多的函数,并且之间也有一些嵌套,因而本例默认给出了匹配 Nuva 函数的正则,顺便看一看本例到底有几个函数。

    3、本例非常典型的显示出 Nuva 语言在和 Html 语言配合使用时构造界面的方便性和实用性,加上 Nuva 语言非常方便的数据绑定和事件驱动编程模型,使用 Nuva 语言可以非常方便的编写 GUI 程序。

    4、本例中字符串的正则操作 RegexMatch 可以返回由传入的 Match 函数返回值连缀的字符串。

    5、以 HtmlForm 为基类的窗口可以接收到一个 OnShow 函数,可以在窗口显示的时候执行一些代码。在本例中,MainForm 的 OnShow 事件执行了默认的正则匹配功能,从而给出了本例使用了多少函数的答案。

【扩展】
    本例可以进一步扩展以增强其实用性,比如可以增加对于正则选项的支持、输出匹配组的详细信息等,从而实现一个真正实用的正则测试程序。
..> 

本例运行结果如下:

posted on 2006-09-01 17:32  Wisdom-zh  阅读(547)  评论(0编辑  收藏  举报

导航