aqubi+shin1

Recent site activity

Adobe Air‎ > ‎

サンプル-正規表現テスト

簡単な正規表現が試せるアプリを作ってみました。

 


ソースをのっけておきます。

MXMLのソース

<?xml version="1.0" encoding="UTF-8"?>
<mx:WindowedApplication layout="absolute" title="ActionScript3.0の正規表現テスト" initialize="loadSample()" alpha="0.7" xmlns:mx="http://www.adobe.com/2006/mxml" height="520" width="500">
<mx:Script source="regexp.as"/>
<mx:Panel title="ActionScript3.0の正規表現テスト" height="500" width="100%">
<mx:HBox height="30" y="90" width="100%" x="0">
<mx:Text text="パターン" height="20" y="0" width="100" x="0"/>
<mx:TextInput text="<p>.*?</p>" id="txtPattern" height="25" y="0" width="100%" x="110"/>
</mx:HBox>
<mx:HBox height="130" y="40" width="470" x="0">
<mx:Label text="オプション" height="20" y="0" width="100" x="0"/>
<mx:VBox height="100%" y="0" width="100%" x="110">
<mx:CheckBox label="複数箇所に一致(g)" selected="true" id="chkG" height="20" width="100%"/>
<mx:CheckBox label="大文字と小文字を判別しない(i)" selected="true" id="chkI" height="20" width="100%"/>
<mx:CheckBox label="$ および ^ はそれぞれ行末と行頭にも一致(m)" id="chkM" height="20" width="100%"/>
<mx:CheckBox label=".(ドット) は改行文字 (\n) にも一致(s)" id="chkS" height="20" width="100%"/>
<mx:CheckBox label="空白を正規表現の中に挿入(e)" id="chkE" height="20" width="100%"/>
</mx:VBox>
</mx:HBox>
<mx:HBox height="80" y="170" width="470" x="0">
<mx:Label text="変換対象文字列" height="20" y="30" width="100" x="0"/>
<mx:TextArea text="<p>こんにちは。</p>" id="areaTarget" height="80" y="0" width="100%" x="110"/>
</mx:HBox>
<mx:HBox height="30" y="169" width="100%" x="156">
<mx:Label text="置換文字列" height="20" y="0" width="100" x="0"/>
<mx:TextInput text="こんばんは¥n" id="txtReplace" height="25" y="0" width="100%" x="110"/>
</mx:HBox>
<mx:HBox height="100%" width="100%">
<mx:Accordion height="100%" width="100%">
<mx:Box label="文字置換の結果" height="100%" width="100%">
<mx:TextArea id="areaResult" height="100%" width="100%"/>
</mx:Box>
</mx:Accordion>
</mx:HBox>
<mx:ControlBar horizontalAlign="center" height="50" y="379" width="100%" x="97">
<mx:Button label="変換" click="regExp()" id="btnApply" height="30" width="80"/>
<mx:Button label="終了" click="windowClose()" id="btnClose" height="30" width="80"/>
</mx:ControlBar>
</mx:Panel>
</mx:WindowedApplication>

ActionScriptのソース

public function loadSample():void { 
 	var str:String = "<p>Hello\n"
        + "again</p>"
        + "<p>Hello</p>";
	areaTarget.text = str;
	txtPattern.text = "<p>.*?</p>";
	txtReplace.text = "おはよ!\n";
}

public function windowClose():void {
	stage.nativeWindow.close();
}

protected function regExp():void {
	var pattern:RegExp = new RegExp(txtPattern.text, createOption()) ;
    areaResult.text= areaTarget.text.replace(pattern, txtReplace.text);
}

private function createOption():String {
 	var option:String = new String();
 	if (chkG.selected) {
 		option = option + "g";
 	}
 	if (chkI.selected) {
 		option = option + "i";
 	}
 	if (chkM.selected) {
 		option = option + "m";
 	}
 	if (chkS.selected) {
 		option = option + "s";
 	}
 	if (chkE.selected) {
 		option = option + "e";
 	} 
 		
 	trace("option=" + option);
 	return option;

}