search
top

Flex Regular Expression Tester – source code

There are a lot of improvements that can be made to this piece of code and when I will get some time I will move forward with the changes. Until then I will post here the source code as it is still small and developers new to the world of regular expressions may learn something from it.

BTW, I have used Flex 4 :)

(updated on 04.15.2009)

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
   3:      xmlns:s="library://ns.adobe.com/flex/spark" pageTitle="Flex Regular Expressions" 
   4:      xmlns:mx="library://ns.adobe.com/flex/halo" height="400" 
   5:      viewSourceURL="srcview/index.html" creationComplete="init()">
   6:      
   7:      <s:layout>
   8:          <s:BasicLayout/>
   9:      </s:layout>
  10:      
  11:      <fx:Script>
  12:          <![CDATA[
  13:          import mx.skins.spark.ButtonSkin;
  14:          private var error:String;
  15:      
  16:          protected function test_btn_clickHandler(event:MouseEvent):void
  17:          {
  18:              var restr:String = regexpInput.text;
  19:              var input:String = textInput.text;
  20:              var igstr:String = modifInput.text;
  21:              
  22:              this.error = null;
  23:              
  24:              if(restr != null && restr != '' && input != null && input != '') {
  25:                  var myPattern:RegExp = new RegExp(restr, igstr);
  26:   
  27:                  var result:Object = myPattern.exec(input);
  28:                  
  29:                  var a:String = '';
  30:                  var b:String = '<br />';
  31:                  var index:int = 0;
  32:   
  33:                  while (result != null) {
  34:                      // copy string from last match
  35:                      a += '<span>' + encode(input.substring(index, result.index)) + '</span>';
  36:                      index = result.index;
  37:                      
  38:                      // copy the matching parts
  39:                      a += '<span color="#339933">' + encode(result[0]) + '</span>'
  40:                      index += result[0].length;                
  41:   
  42:                      // create the trace string
  43:                      b += '<br /> ' + result.index + '\t'; 
  44:                      for(var i:int = 0; i<result.length; i++) {
  45:                          b += result[i] + ',';
  46:                      }
  47:                      
  48:                      if(igstr.indexOf('g') == -1) {
  49:                          a+= input.substr(index);
  50:                          break;
  51:                      }
  52:                      result = myPattern.exec(input);
  53:                  }
  54:                              
  55:                  a += '';
  56:                  resultText.content = a + b;
  57:              }
  58:          }
  59:                      
  60:          protected function encode(string:String):String {
  61:              var htmlString:String = string.replace(/\n/g, "<br />");
  62:   
  63:              return htmlString;
  64:          }
  65:          ]]>
  66:      </fx:Script>
  67:   
  68:      <s:Panel id="contentPane" width="100%" height="100%" left="0" 
  69:          title="Flex Regular Expressions" >
  70:          
  71:          <s:TextArea left="10" right="10" height="150" top="10" 
  72:              text="She sells seashells by the seashore" id="textInput" />
  73:          <s:TextInput text="(\w*)sh(\w*)" id="regexpInput" left="10" top="168" right="175"/>
  74:          <s:TextInput id="modifInput" text="ig" right="85" top="168" width="78"/>
  75:          <s:Button label="test" id="test_btn" right="10" top="168" 
  76:              click="test_btn_clickHandler(event)"/>
  77:          <s:TextArea id="resultText" left="10" right="10" top="198" bottom="10"/>
  78:      </s:Panel>
  79:      
  80:  </s:Application>

3 Responses to “Flex Regular Expression Tester – source code”

  1. [...] > Flex Regular Expression Tester – source code | Remus Stratulat – On the Stre@m [...]

  2. mrm says:

    Maybe you should check out RegExr — http://www.gskinner.com/RegExr/
    There’s also an AIR version — http://www.gskinner.com/RegExr/desktop/

Leave a Reply

top