//模式匹配工具
//模式匹配工具//模式匹配辅助工具

/************************************
Author		: ECranesoft Lab
Time		: March 12, 2010
Version		: V1.0.0.1021
Description : This demo applets is about Pattern and UI.
************************************/

import win.ui; 
import win.ui.menu;
import win.ole.image; 
import fsys.dlg;
import win.clip;
/*DSG{{*/
var winform = win.form(parent=...; bgcolor=16777215;bottom=584;max=1;text="模式匹配辅助工具";left=0;border="resizable";min=1;image=$"\.res\pattern.jpg";title="AAuto Form";right=840;top=0;style=0;exmode="";cls="AFORM" )
winform.add( 
txtSource={ dl=1;vscroll=1;right=566;left=18;dt=1;db=1;cls="richedit";bottom=572;text="请输入测试文本...";multiline=1;top=190;z=34;ah=1;hscroll=1;edge=1;aw=1 };
chkMatchGlobal={ dl=1;checked=1;bottom=107;color=16777215;right=650;left=574;dt=1;top=91;text="全局匹配";transparent=1;tabstop=1;z=28;cls="checkbox";hide=1 };
button8={ dl=1;bottom=77;color=0;text="串结束＄";left=96;dt=1;top=53;tabstop=1;align="";right=168;z=4;cls="button" };
button14={ dl=1;bottom=107;color=0;text="数字▼";left=17;dt=1;top=83;tabstop=1;align="";right=78;z=25;cls="button" };
button={ dl=1;bottom=77;color=0;text="转义符 \";left=339;dt=1;top=53;tabstop=1;align="";right=411;z=6;cls="button" };
groupbox={ dl=1;bgcolor=14215660;color=0;right=837;left=10;dt=1;cls="groupbox";z=0;bottom=146;text="匹配模式:";top=40;transparent=1;aw=1;edge=1;tabstop=1 };
button18={ dl=1;bottom=107;color=0;text="I D";left=227;dt=1;top=83;tabstop=1;align="";right=275;z=7;cls="button" };
txtPath={ aw=1;bgcolor=16777215;bottom=184;color=8421504;right=440;left=120;dt=1;top=160;text="选择浏览读取要匹配的文本...";tabstop=1;multiline=1;z=8;edge=1;cls="edit" };
lstMatch={ dr=1;ah=1;bgcolor=16777215;vscroll=1;color=8421504;right=823;left=585;dt=1;db=1;cls="listbox";top=163;bottom=572;text="listbox";
items={ "这里显示模式匹配结果:" };aw=1;z=32;hscroll=1;edge=1;tabstop=1 };
chkReplace={ dl=1;bgcolor=14215660;bottom=107;color=16777215;right=544;left=488;dt=1;top=91;z=9;text="替 换";transparent=1;tabstop=1;cls="radiobutton" };
button13={ dl=1;bottom=77;color=0;text="字符类▼";left=423;dt=1;top=53;tabstop=1;align="";right=495;z=10;cls="button" };
btnSave={ bgcolor=14215660;bottom=136;color=0;text="保存代码";left=598;dt=1;top=112;disabled=1;z=11;align="";right=662;tabstop=1;cls="button" };
button19={ dl=1;bottom=107;color=0;text="更多▼";left=288;dt=1;top=83;tabstop=1;align="";right=336;z=12;cls="button" };
button3={ bgcolor=14215660;bottom=136;color=0;text="复制代码";left=522;dt=1;top=112;disabled=1;z=13;align="";right=590;tabstop=1;cls="button" };
button6={ dl=1;bottom=77;color=0;text="字符串 <>";left=665;dt=1;top=53;tabstop=1;align="";right=737;z=14;cls="button" };
groupbox2={ dr=1;ah=1;bgcolor=14215660;color=0;right=830;left=579;dt=1;db=1;cls="groupbox";aw=1;bottom=578;text="匹配结果:";top=144;tabstop=1;z=1;edge=1;transparent=1 };
txtRep={ dl=1;bgcolor=16777215;color=8421504;right=656;left=552;dt=1;cls="edit";hide=1;bottom=112;text="请输入替换内容";multiline=1;top=90;tabstop=1;edge=1;z=15 };
button10={ dl=1;bottom=77;color=0;text="边界 ![]";left=176;dt=1;top=53;tabstop=1;align="";right=248;z=27;cls="button" };
txtPattern={ aw=1;bgcolor=16777215;bottom=138;color=8421504;right=424;left=56;dt=1;top=114;text="请输入模式表达式...";tabstop=1;multiline=1;z=33;edge=1;cls="edit" };
button17={ dl=1;bottom=107;color=0;text="网 址";left=169;dt=1;top=83;tabstop=1;align="";right=217;z=22;cls="button" };
txtBrowser={ bgcolor=14215660;bottom=182;color=0;right=505;left=449;dt=1;top=158;align="";z=18;text="浏览...";tabstop=1;aw=1;cls="button" };
chkFind={ dl=1;checked=1;bottom=107;color=16777215;right=416;left=352;dt=1;top=91;text="查 找";transparent=1;bgcolor=14215660;tabstop=1;z=19;cls="radiobutton" };
txtRepCount={ dl=1;bgcolor=16777215;color=8421504;right=817;left=737;dt=1;cls="edit";hide=1;bottom=112;text="默认替换全部";multiline=1;top=89;tabstop=1;edge=1;z=21 };
groupbox3={ dl=1;bgcolor=14215660;color=0;right=575;left=9;dt=1;db=1;cls="groupbox";ah=1;z=2;bottom=579;text="测试文本:";top=149;aw=1;tabstop=1;edge=1;transparent=1 };
button12={ dl=1;bottom=77;color=0;text="补 集 [^]";left=584;dt=1;top=53;tabstop=1;align="";right=656;z=23;cls="button" };
button2={ bgcolor=14215660;bottom=135;color=0;text="测试模式";left=440;dt=1;top=114;tabstop=1;align="";right=512;z=17;cls="button" };
button15={ dl=1;bottom=108;color=0;text="字符串▼";left=89;dt=1;top=82;tabstop=1;align="";right=152;z=5;cls="button" };
txtResult={ bgcolor=16777215;vscroll=1;color=8421504;right=815;left=585;hide=1;bottom=569;text="显示模式替换结果...";multiline=1;top=163;z=20;cls="edit";hscroll=1;edge=1;tabstop=1 };
button11={ dl=1;bottom=77;color=0;text="字符集 []";left=503;dt=1;top=53;tabstop=1;align="";right=575;z=26;cls="button" };
chkMatch={ dl=1;bgcolor=14215660;bottom=107;color=16777215;right=464;left=416;dt=1;top=91;z=16;text="匹 配";transparent=1;tabstop=1;cls="radiobutton" };
button16={ dl=1;bottom=77;color=0;text="修饰符 ▼";left=748;dt=1;top=53;tabstop=1;align="";right=828;z=3;cls="button" };
button7={ dl=1;bottom=77;color=0;text="对称 %";left=257;dt=1;top=53;tabstop=1;align="";right=329;z=29;cls="button" };
button9={ dl=1;bottom=77;color=0;text="串结开始 ^";left=17;dt=1;top=53;tabstop=1;align="";right=89;z=30;cls="button" };
lbReplace={ dl=1;bgcolor=14215660;color=16777215;right=729;left=673;dt=1;cls="static";hide=1;z=31;bottom=107;align="";text="替换次数:";top=91;tabstop=1;transparent=1 };
static={ bgcolor=14215660;bottom=181;align="";text="文件路径：";left=47;dt=1;top=165;color=16777215;tabstop=1;right=114;transparent=1;z=24;cls="static" }
)
/*}}*/

winform.chkMatchGlobal.theme = false;
winform.chkFind.theme =false;
winform.chkReplace.theme = false;
winform.chkMatch.theme = false;
winform.txtSource.limit = 6500000;

/*
path = io.splitpath(...)
winform.image = path.dir + ".res\pattern.jpg"  
*/;  

winform.lstMatch.oncommand = function(id,event){
     if( evnet = ::_LBN_SELCANCEL ){
	 	var index = winform.lstMatch.selIndex;
	 	var strSource = winform.txtSource.text;
	 	strSource = string.replace(strSource,'\r\n','\n');
	 	
     	var pos, pos2 = 1, 1
     	for(i=0;index;1){
        	pos, pos2 = string.find(strSource,winform.txtPattern.text,pos2+1 );
        	if( !pos )return;
    	}
    	if( !pos or (pos2 != 1) ){
    	    var strtest = string.left( strSource,pos-1);
    	    //中文字在richedit按字符计算为一个字符，在非两个字节
    	    for str in string.gmatch(strtest,'(<\xA1-\xF7\xA1-\xFE>)') { 
   			pos--;
   			pos2--; 
			}
			strtest = string.sub( strSource,pos,pos2);
    	    for str in string.gmatch(strtest,'(<\xA1-\xF7\xA1-\xFE>)') {  
   			pos2--; 
			}
    		winform.txtSource.setsel( pos-1, pos2);
    		var line = winform.txtSource.lineFromChar( pos );  
    		winform.txtSource.scrollCaret();
    	}
	}
	//无返回值则继续调用默认回调函数
}//endproc
 

winform.button15.oncommand = function(hwnd,event){
	//win.msgbox( winform.button15.text );
	winform.txtPattern.text = "";
	var popmenu = win.ui.popmenu(winform); 
	popmenu.add('对称双引号',insertExpressionMenu( '%""'));
	popmenu.add('对称单引号',insertExpressionMenu( "%''"));
	popmenu.add('英文字母串',insertExpressionMenu( "^[A-Za-z]+$"));
	popmenu.add('大写英文串',insertExpressionMenu( "^[A-Z]+$"));
	popmenu.add('小写英文串',insertExpressionMenu( "^[a-z]+$"));
	popmenu.add('数字和字母串',insertExpressionMenu( "^[A-Za-z0-9]+$"));
	popmenu.add('数字、字母或下划线串',insertExpressionMenu( "^\w+$") );
	popmenu.add('邮箱地址',insertExpressionMenu( "\w+[\w\-\.]+\w@\w+[\w\-]*\w\.[\w\-\.]*\w{2,}") );
	//添加按钮弹出菜单
	popmenu.popup(winform.button15.right - 80 ,winform.button15.bottom);
}//endproc

winform.button14.oncommand = function(hwnd,event){
	//win.msgbox( winform.button14.text );
	winform.txtPattern.text = "";
	var popmenu = win.ui.popmenu(winform);
	popmenu.add('匹配正整数',insertExpressionMenu( "^[1-9]\d*$"));
	popmenu.add('匹配负整数',insertExpressionMenu( "^\-[1-9]\d*$"));
	popmenu.add('匹配整数',insertExpressionMenu( "^\-?[1-9]\d*$"));
	popmenu.add('匹配非负整数',insertExpressionMenu( "^[1-9]\d*$"));
	popmenu.add('匹配非正整数',insertExpressionMenu( "^\-[1-9]\d*$"));
	popmenu.add('匹配正浮点数',insertExpressionMenu( "^\d*\.\d*") );
	popmenu.add('匹配负浮点数',insertExpressionMenu( "^\-^\d*\.\d*$") );
	popmenu.add('匹配浮点数',insertExpressionMenu( "^\-?^\d*\.\d*$") );
	//添加按钮弹出菜单
	popmenu.popup(winform.button14.right - 80 ,winform.button14.bottom);
}//endproc

winform.button19.oncommand = function(hwnd,event){ 
    var regex_href = //\s*href\s*=\s*\"?\'?([:\w\./@]+)\"?\'?\s*
	winform.txtPattern.text = "";
	var popmenu = win.ui.popmenu(winform);
	popmenu.add('PASCAL语句块',insertExpressionMenu( "%<\Wbegin\W><\Wend\W>?"));
	popmenu.add('IP',insertExpressionMenu( "\d+\.\d+\.\d+\.\d+"));
	popmenu.add('QQ号',insertExpressionMenu( "[1-9][0-9]{4,15}"));
	popmenu.add('中文',insertExpressionMenu( "(<\xA1-\xF7\xA1-\xFE>+)"));
	popmenu.add('空行',insertExpressionMenu( "\n\s*[\r\n]"));
	popmenu.add('邮编',insertExpressionMenu( "[1-9]\d{5}![\d]"));
	popmenu.add('HTML',insertExpressionMenu( "(\<(\S*?)[^>]*\>(.*?)\</\2\>)"));
	popmenu.add('超链接',insertExpressionMenu( regex_href ) );
	popmenu.add('身份证号',insertExpressionMenu( "\d{15,18}") );
	//添加按钮弹出菜单
	popmenu.popup(winform.button19.right - 45 ,winform.button19.bottom);
}//endproc

winform.button18.oncommand = function(hwnd,event){
	//win.msgbox( winform.button18.text );
	winform.txtPattern.text = "";
	insertExpression( "^[a-zA-Z][a-zA-Z0-9_]{4,15}$" );
}//endproc

winform.button17.oncommand = function(hwnd,event){
	//win.msgbox( winform.button17.text );
	winform.txtPattern.text = "";
	insertExpression( "[a-zA-z]+://[^\s]*" );
}//endproc

winform.button12.oncommand = function(hwnd,event){
	//win.msgbox( winform.button12.text );
	insertExpression( "[^]" );
}//endproc

winform.button11.oncommand = function(hwnd,event){
	//win.msgbox( winform.button11.text );
	insertExpression( "[]" );
}//endproc

winform.button10.oncommand = function(hwnd,event){
	//win.msgbox( winform.button10.text );
	insertExpression( "![]" );
}//endproc

winform.button9.oncommand = function(hwnd,event){
	//win.msgbox( winform.button9.text );
	insertExpression( "^" );
}//endproc

winform.button8.oncommand = function(hwnd,event){
	//win.msgbox( winform.button8.text );
	insertExpression( "$" );
}//endproc

winform.button7.oncommand = function(hwnd,event){
	//win.msgbox( winform.button7.text );
	insertExpression( "%" );
}//endproc

winform.button6.oncommand = function(hwnd,event){
	//win.msgbox( winform.button6.text );
	insertExpression( "<>" );
}//endproc

insertExpression =  function( strInsert, cursorPos = 0 ){
	//恢复编辑框字体为正常属性
	winform.txtPattern.font = LOGFONT( h=-12;name="黑体" );
   	winform.txtPattern.color = ::RGB(0,0,0);
	winform.txtPattern.redraw();

	if( winform.txtPattern.text == "请输入模式表达式..." ){
    		winform.txtPattern.text = "";
    	}
    var strExpression = winform.txtPattern.text;
  
    var strLastchar = string.right(strExpression,1);
    var strfirstchar = string.left( strInsert,1);
       
    if( strfirstchar=="<" or strfirstchar=="[" or strfirstchar=="!")
       strExpression += strInsert;
    else if( strLastchar == ']') {
    	strExpression = string.trimright( strExpression ,']');
    	strExpression += (strInsert +']');
    }
    else if( strLastchar == '>' ){
    	strExpression = string.left( strExpression,#strExpression-1)
    	strExpression += (strInsert +'>');
    }
    else
    	strExpression += strInsert;
    
	winform.txtPattern.text = strExpression;
}

insertExpressionMenu = function( strInsert, cursorPos = 0 ){
    	return function(){
    				insertExpression( strInsert, cursorPos );
    			} 
}

winform.button.oncommand = function(hwnd,event){
	//win.msgbox( winform.button.text );
	insertExpression( "\");
}//endproc


winform.button16.oncommand = function(hwnd,event){
	//win.msgbox( winform.button16.text );	
	var popmenu = win.ui.popmenu(winform); 
	popmenu.add('P?  匹配p模式零或一次',insertExpressionMenu( "?"));
	popmenu.add() 
	popmenu.add('P+  长匹配P模式一次或多次',insertExpressionMenu( "+"));
	popmenu.add('P+?  短匹配P模式一次或多次',insertExpressionMenu( "+?"));
	popmenu.add()
	popmenu.add('P*  长匹配P模式零次以上',insertExpressionMenu( "*"));
	popmenu.add('P*?  短匹配P模式零次以上',insertExpressionMenu( "*?"));
	popmenu.add()
	popmenu.add('P{n,m}  匹配p最少n最多m次',insertExpressionMenu( "{n,m}") );
	popmenu.add()
	popmenu.add('P1|P2 匹配P1或P2其中之一',insertExpressionMenu( "|")); 
	//添加按钮弹出菜单
	popmenu.popup(winform.button16.right - 80 ,winform.button16.bottom);
}//endproc

winform.button13.oncommand = function(hwnd,event){
	//win.msgbox( winform.button13.text );
	var popmenu1 = win.ui.popmenu(winform);begin
	popmenu1.add('. 任意字符',insertExpressionMenu( ".") );
	popmenu1.add('\\a 字母',insertExpressionMenu( "\a"));
	popmenu1.add('\\c 控制字符',insertExpressionMenu( "\c"));
	popmenu1.add('\\d 数字',insertExpressionMenu( "\d"));
	popmenu1.add('\\i 是否ASCII字符',insertExpressionMenu( "\i"));
	popmenu1.add('\\l 小写字母',insertExpressionMenu( "\l"));
	popmenu1.add('\\p 标点字符',insertExpressionMenu( "\p"));
	popmenu1.add('\\s 空白符',insertExpressionMenu( "\s"));
	popmenu1.add('\\u 大写字母',insertExpressionMenu( "\u"));
	popmenu1.add('\\w 字母和数字',insertExpressionMenu( "\w"));
	popmenu1.add('\\x 十六进制数字',insertExpressionMenu( "\x"));
	popmenu1.add('\\n 换行符',insertExpressionMenu( "\n"));
	popmenu1.add('\\r 回车符',insertExpressionMenu( "\r"));
	popmenu1.add('\\f 换页符',insertExpressionMenu( "\f"));
	popmenu1.add('\\v 匹配一个垂直制表符 ',insertExpressionMenu( "\v"));
	popmenu1.add('\\z\\0 字节码为0的字符',insertExpressionMenu( "\z"));
	popmenu1.add('注：大写表示小写集合的补集',function(){});
end;//添加按钮弹出菜单

	popmenu1.popup(winform.button13.right - 70 ,winform.button13.bottom);
	
}//endproc


winform.btnSave.oncommand = function(hwnd,event){     //保存匹配结果
	//win.msgbox( winform.btnSave.text );
	var strPath = winform.txtPath.text;
	var strBuffer;
	
	if( not winform.txtResult.hide ){
		strBuffer = winform.txtResult.text;
	}
	else{
    	var tab = winform.lstMatch.items; 
		strBuffer = string.join( tab, ';\r\n');
	}
	
	if( strPath != ""){
		var filename = fsys.dlg.save( "文本文件|*.txt|所有文件|*.*||" ,"保存模式测试结果", strPath);
		if(filename){
	   	string.save(filename, strBuffer );
		}
	}
}//endproc

//动态显示或隐藏控件
winform.chkFind.oncommand = function(hwnd,event){
	//win.msgbox( winform.chkFind.text );
	winform.chkMatchGlobal.hide = true;
	winform.txtRep.hide = true;
	winform.txtRepCount.hide = true;
	winform.lbReplace.hide = true;
	winform.txtResult.hide = true;
	winform.lstMatch.hide = false;;
	winform.redraw( );
}//endproc

winform.chkReplace.oncommand = function(hwnd,event){
	//win.msgbox( winform.chkReplace.text );
	winform.chkMatchGlobal.hide = true;
	winform.txtRep.hide = false;;
	winform.txtRepCount.hide = false;;
	winform.lbReplace.hide = false;;
	winform.txtResult.hide = false;;
	winform.lstMatch.hide = true;
	winform.redraw();
}//endproc

winform.chkMatch.oncommand = function(hwnd,event){
	//win.msgbox( winform.chkMatch.text );
	winform.chkMatchGlobal.hide = false;;
	winform.txtRep.hide = true;
	winform.txtRepCount.hide = true;
	winform.lbReplace.hide = true;
	winform.txtResult.hide = true;
	winform.lstMatch.hide = false;;
	winform.redraw();
}//endproc


winform.button3.oncommand = function(hwnd,event){    //将内容复制到剪贴板
	//win.msgbox( winform.button3.text );
	win.clip.write( winform.editAAutoCode   );
}//endproc


//编辑框初始文字为灰色,鼠标单击之后恢复正常
winform.txtResult.oncommand = function(hwnd,event){
	//win.msgbox( winform.txtResult.text );
	if( winform.txtResult.text == "显示模式匹配结果..."){
		winform.txtResult.text = "";
		winform.txtResult.color = 0;
		winform.txtResult.redraw();
	}
}//endproc
  
winform.txtRepCount.oncommand = function(hwnd,event){
	//win.msgbox( winform.txtRepCount.text );
	if( winform.txtRepCount.text == "默认替换全部"){
		winform.txtRepCount.text = "";
		winform.txtRepCount.color = 0;
		winform.txtRepCount.redraw();
	}
}//endproc

winform.txtRep.oncommand = function(hwnd,event){
	//win.msgbox( winform.txtRep.text );
	if( winform.txtRep.text == "请输入替换内容"){
		winform.txtRep.text = "";
		winform.txtRep.color = 0;
		winform.txtRep.redraw();
	}
}//endproc

winform.txtPattern.oncommand = function(hwnd,event){
	//win.msgbox( winform.txtPattern.text );
	if( winform.txtPattern.text == "请输入模式表达式..."){
		winform.txtPattern.text = "";
		winform.txtPattern.color = 0;
		winform.txtPattern.redraw();
	}
}//endproc

winform.txtPath.oncommand = function(hwnd,event){
	//win.msgbox( winform.txtPath.text );
	if( winform.txtPath.text == "选择浏览读取要匹配的文本..."){
		winform.txtPath.text = "";
		winform.txtPath.color = 0;
		winform.txtPath.redraw();
	}
}//endproc

winform.txtSource.oncommand = function(hwnd,event){
 	//win.msgbox( winform.txtSource.text );
	if( winform.txtSource.text == "请输入测试文本..."){
		winform.txtSource.text = "";
		winform.txtSource.color = 0;
		winform.txtSource.redraw();
	}
	
}//endproc

winform.txtBrowser.oncommand = function(hwnd,event){         //读取测试文本
	//win.msgbox( winform.txtBrowser.text );
	var strTemp = winform.txtPath.text;
	var strfile = fsys.dlg.open( "文本文件|*.txt|所有文件|*.*||", "选择要匹配的文本文件" ); 
	if(strfile){
		assert( string.load( strfile ) );
		winform.txtSource.text = string.load( strfile );
        winform.txtPath.text = strfile;
        
        //恢复编辑框字体为正常属性
		winform.txtPath.font =  LOGFONT( h=-12;name="黑体" );
   		winform.txtPath.color = ::RGB(0,0,0);
   		winform.txtPath.redraw();
   		
   		winform.txtSource.font =  LOGFONT( h=-12;name="黑体" );
   		winform.txtSource.color = ::RGB(0,0,0);
   		winform.txtSource.redraw();
    }
}//endproc


//创建弹出菜单
var popmenu = win.ui.popmenu(winform);
popmenu.add('关于 本工具(&A)',function(){ win.msgbox( '  模式匹配辅助工具 1.0.0.1021    \nCopyright (c) ECranesoft Lab 2010 \n','This tool is created by  AAuto !' ) });
popmenu.add('访问AAuto官方论坛(&B)',function(){ ::ShellExecute(0, "", "http://www.ecranesoft.com/bbs", "", "", 0)})
popmenu.add();
popmenu.add('退出(&E)',function(){ winform.close() })
//var menu = win.ui.menu(winform);//创建主菜单
//menu.add('file',popmenu)

Hook = function(str,regex){
   var items = {}
   string.replace(str,regex,function( ... ){
       var matchs = {...};
       table.push( items, table.tostring(matchs) );
   } );
   
   if( #items ==0  ){
    	winform.lstMatch.items = {"匹配结果为空..."};
    	return ; 
   }
   
   winform.lstMatch.items = items;
   
   return items; 
}


patternReplace = function( strTestText,regexExpression ,regexExpressionTemplate,replaceText,replacecount ){
		var strReplaceCode;
			//这样书写写是为了生成的代码的格式比较工整
		strAAutoCode =
/************************** 
var strTestText = /*************
%s
*************/;
var regexExpression = %s ;
var replaceText = "%s";
**************************/
     
		if( replacecount == "默认替换全部" ){
			strtemp = string.replace( strTestText, regexExpression, replaceText );
			strReplaceCode = "strReplaceResult = string.replace( strTestText, regexExpression, replaceText);"
			strAAutoCode = string.format(strAAutoCode+strReplaceCode , strTestText, regexExpressionTemplate  ,replaceText );
		}
		else if( tonumber( replacecount )  ){
			strtemp = string.replace( strTestText,regexExpression,replaceText,replacecount);
			strReplaceCode = /*****var replacecount = "%s";
									strReplaceResult = string.replace( strTestText,regexExpression,replaceText,replacecount);
									*****/
			strAAutoCode = string.format( strAAutoCode+strReplaceCode , strTestText,regexExpressionTemplate  ,replaceText, replacecount )
		}
		else{
			strtemp = "错误,请输入替换次数,必须为正整数!"
			strAAutoCode = "错误,请输入替换次数,必须为正整数!"
			
			return strtemp, strAAutoCode ;
		}
		
		return strtemp, strAAutoCode;
	}
	
	
patternMatch = function( strTestText, regexExpression,regexExpressionTemplate ){
	
 		var strMatchCode;
 		//这样书写写是为了生成的代码的格式比较工整
		strAAutoCode =
/************************** 
var strTestText = /*************
%s
*************/;
var regexExpression =  %s ;
**************************/
		//清空所有列表项
     	winform.lstMatch.clear();
     	
		if( winform.chkMatchGlobal.checked == true ){
		Hook(strTestText, regexExpression );
		strtemp = string.gmatch( strTestText, regexExpression );	
		strMatchCode = /*****for strGmatchResult in string.gmatch( strTestText, regexExpression ){
//strGmatchResult为匹配的结果
}
*****/
		}
		else{
			strtemp = string.match( strTestText, regexExpression );
			var items = {}
       		table.push( items, strtemp);
			winform.lstMatch.items = items;
			
			strMatchCode = "strMatchResult = string.match( strTestText, regexExpression );"
		}
		
		strAAutoCode = string.format(strAAutoCode+strMatchCode , strTestText, regexExpressionTemplate );
		
		return strAAutoCode; 
	}
	
	
patternFind = function( strTestText, regexExpression ,regexExpressionTemplate){
	
 		//这样书写写是为了生成的代码的格式比较工整
		strAAutoCode =
/************************** 
var strTestText = /*************
%s
*************/; 
var regexExpression =  %s ;
i,j = string.find( strTestText,  regexExpression );

//使用模式匹配查找字符串并返回起始位置(i),结束位置(j);
**************************/
	i,j = string.find( strTestText, regexExpression );  //使用模式匹配查找字符串并返回起始位置(i),结束位置(j);
	if( i ){
		strtemp1 = string.format('串起始位置i="%d":', i);
		strtemp2 = string.format('串结束位置j="%d":', j);
		//同时间将找到的串标记为选中状态
		winform.txtSource.setsel( i-1, j );
	}
	else{
		strtemp1 = "没找到相应串!";
		strtemp2 = strtemp1;
	}
	//清空所有列表项
    winform.lstMatch.clear();
	var items = {}
    table.push( items, strtemp2);
    table.push( items, strtemp1);
	winform.lstMatch.items = items;
	
	strAAutoCode = string.format( strAAutoCode, strTestText, regexExpressionTemplate );
	return  strAAutoCode; 
	}


winform.button2.oncommand = function(hwnd,event){
	//win.msgbox( winform.button2.text );
	//处理模式匹配过程
	var strtemp,strAAutoCode = "请输入模式测试内容", "匹配内容和匹配式内容为空，生成代码失败!";
	var regexExpression,strTestText = winform.txtPattern.text,winform.txtSource.text;
	var patternType = ( winform.chkFind.checked ? winform.chkFind.id) or ( winform.chkMatch.checked ? winform.chkMatch.id) or ( winform.chkReplace.checked ? winform.chkReplace.id );
	var replaceText, replacecount = winform.txtRep.text,   winform.txtRepCount.text ;
 
    var regexExpressionTemplate = '"' + regexExpression +  '"';
	if( ..string.find(regexExpression,"@\x") ){
	     regexExpressionTemplate = "'" + regexExpression +  "'" 
	     var f =  loadcode("return '" + regexExpression ++ "'")
	     regexExpression = f(); 
	}
	else if( ..string.find(regexExpression,'["\']') ){
	         regexExpressionTemplate = "//" + regexExpression ; 
	}
	
 
	select(patternType) {
	
		case winform.chkFind.id					//模式查找功能
		{
			if( strTestText != "" and regexExpression != "" ){
			strAAutoCode = patternFind( strTestText, regexExpression ,regexExpressionTemplate)
			}
			else{
			strAAutoCode = "请输入要查找的目标内容和匹配式";
			}		
		}
		case  winform.chkMatch.id       		 //模式匹配功能
		{
			strAAutoCode = patternMatch( strTestText, regexExpression ,regexExpressionTemplate);
			
		}
		case  winform.chkReplace.id			//模式替换功能
		{	
			if( strTestText != "" and regexExpression != "" ){
				strtemp, strAAutoCode = patternReplace( strTestText, regexExpression,regexExpressionTemplate, replaceText, replacecount );
				winform.txtResult.text = strtemp;
			}
			else{
				strAAutoCode = "错误,请输入要查找的目标内容和匹配式";
				winform.txtResult.text = "错误,请输入需要替换的目标文本!"
			}
			
		}
	}//end select...case
	
	
	//恢复代码和结果编辑框字体颜色
	
	winform.lstMatch.font = LOGFONT( h=-12;name="黑体" );
   	winform.lstMatch.color = ::RGB(0,0,0);
   	winform.lstMatch.redraw();
 
   		
   	winform.txtResult.font = LOGFONT( h=-12;name="黑体" );
   	winform.txtResult.color = ::RGB(0,0,0);
   	winform.txtResult.redraw();

	//显示自动生成的代码
	strAAutoCode = string.replace( strAAutoCode, '\n', '\r\n' );
	winform.editAAutoCode  = strAAutoCode;
	
	//启用复制代码和保存结果按钮
	winform.button3.disabled = false;
	winform.btnSave.disabled = false;
}//endproc


winform.wndproc = function(hwnd,message,wparam,lparam){
select(message) {//判断消息类型
	case 0x201/*_WM_LBUTTONDOWN*/ //鼠标按下
	{  
	    
	}
	case 0x202/*_WM_LBUTTONUP*/ //鼠标弹起
	{ }
	case 0x3/*_WM_MOVE*/ //鼠标移动
	{ }
	case 0x10/*_WM_CLOSE*///窗口关闭消息
	{ }
	case 0x205/*_WM_RBUTTONUP*///窗口关闭消息
	{
	popmenu.popup(::LOWORD(lparam), ::HIWORD(lparam));//弹出菜单
	}
	else
	{ }
}
}//endproc


winform.show(true) 
win.loopMessage( winform );
