asp下常用正則表達式及字符串驗證方法

asp下常用正則表達式及字符串驗證方法
'==================================================
'正則表達式
'===================================================


 '常用正則表達式模式
const z_Pat1='^/w+$'                   '匹配有字母,數字,下滑線組成的字符串
const z_PatSW='^[/x00-/xff]+$'         '匹配所有單字節長度的字符組成的字符串
const z_PatDW='^[^/x00-/xff]+$'        '匹配所有雙字節長度的字符組成的字符串
const z_PatDW2='[^/x00-/xff]+'             '字符串是否含有雙字節字
const z_PatFileName1='^((/w+)(/.{1})(/w+))$'   '驗證一個文件名,他是由字母,數字,下滑線組成
const z_PatDate1='^19/d{2}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3([0|1])))$'     '匹配日期(1900-1999)
const z_PatDate2='^20/d{2}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3([0|1])))$'     '匹配日期(2000-2999)
const z_PatternDateTime='^(1|2/d{3}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3([0|1]))))( (/d{2}):(/d{2}):(/d{2}))?$'  '匹配日期時間

const z_PatInt='^/d+$'                     '驗證整數數字
const z_PatNum='^/d+(/.{1}/d+)?$'          '數字


'驗證指定的字符串是否符合指定的模式
'利用正則表達式
 function CheckPattern(str,pat)
    set r=new regexp
    r.Pattern=pat
    r.IgnoreCase=false
    r.Global=false
    if r.Test(str) then
        CheckPattern=true
    else
        CheckPattern=false
    end if
 end function  
=====================================
正則表達式用於字符串處理、表單驗證等場合,實用高效。現將一些常用的表達式收集於此,以備不時之需。


匹配中文字符的正則表達式: [/u4e00-/u9fa5]
評註:匹配中文還真是個頭疼的事,有了這個表達式就好辦了

匹配雙字節字符(包括漢字在內):[^/x00-/xff]
評註:可以用來計算字符串的長度(一個雙字節字符長度計2,ASCII字符計1)

匹配空白行的正則表達式:/n/s*/r
評註:可以用來刪除空白行

匹配HTML標記的正則表達式:<(/S*?)[^>]*>.*?<//1>|<.*? />
評註:網上流傳的版本太糟糕,上面這個也僅僅能匹配部分,對於複雜的嵌套標記依舊無能爲力

匹配首尾空白字符的正則表達式:^/s*|/s*$
評註:可以用來刪除行首行尾的空白字符(包括空格、製表符、換頁符等等),非常有用的表達式

匹配Email地址的正則表達式:/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*
評註:表單驗證時很實用

匹配網址URL的正則表達式:[a-zA-z]+://[^/s]*
評註:網上流傳的版本功能很有限,上面這個基本可以滿足需求

匹配帳號是否合法(字母開頭,允許5-16字節,允許字母數字下劃線):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
評註:表單驗證時很實用

匹配國內電話號碼:/d{3}-/d{8}|/d{4}-/d{7}
評註:匹配形式如 0511-4405222 或 021-87888822

匹配騰訊QQ號:[1-9][0-9]{4,}
評註:騰訊QQ號從10000開始

匹配中國郵政編碼:[1-9]/d{5}(?!/d)
評註:中國郵政編碼爲6位數字

匹配身份證:/d{15}|/d{18}
評註:中國的身份證爲15位或18位

匹配ip地址:/d+/./d+/./d+/./d+
評註:提取ip地址時有用

匹配特定數字:
^[1-9]/d*$    //匹配正整數
^-[1-9]/d*$   //匹配負整數
^-?[1-9]/d*$   //匹配整數
^[1-9]/d*|0$  //匹配非負整數(正整數 + 0)
^-[1-9]/d*|0$   //匹配非正整數(負整數 + 0)
^[1-9]/d*/./d*|0/./d*[1-9]/d*$   //匹配正浮點數
^-([1-9]/d*/./d*|0/./d*[1-9]/d*)$  //匹配負浮點數
^-?([1-9]/d*/./d*|0/./d*[1-9]/d*|0?/.0+|0)$  //匹配浮點數
^[1-9]/d*/./d*|0/./d*[1-9]/d*|0?/.0+|0$   //匹配非負浮點數(正浮點數 + 0)
^(-([1-9]/d*/./d*|0/./d*[1-9]/d*))|0?/.0+|0$  //匹配非正浮點數(負浮點數 + 0)
評註:處理大量數據時有用,具體應用時注意修正

匹配特定字符串:
^[A-Za-z]+$  //匹配由26個英文字母組成的字符串
^[A-Z]+$  //匹配由26個英文字母的大寫組成的字符串
^[a-z]+$  //匹配由26個英文字母的小寫組成的字符串
^[A-Za-z0-9]+$  //匹配由數字和26個英文字母組成的字符串
^/w+$  //匹配由數字、26個英文字母或者下劃線組成的字符串
評註:最基本也是最常用的一些表達式
========================================
explode preg_match

 

if(0!=$k) {
                                $oldstr = array('/'','(',');','/n','/r','array','Array');
                                $newstr = array('','','','','','','');
                                $messages = str_replace($oldstr,$newstr,$v);
//                                $messages = strtolower($messages);
                                $rule="/"/;[a-zA-Z0-9]{1,}/:[a-zA-Z0-9{1,}/:/"[a-zA-Z0-9]{1,}/"/;[a-zA-Z0-9]{1,}/:[a-zA-Z0-9]{1,}/:/"";
                                $messages=ereg_replace($rule,"",$messages);
                                $messages = explode(',',$messages);
                                $tmp_datetime = explode(' ',$messages[1]);
                                $temp_date = explode('-',$tmp_datetime[0]);
                                $temp_time = explode(':',$tmp_datetime[1]);
                                $tmp_year        = $temp_date[0];
                                $tmp_month        = $temp_date[1];
                                $tmp_day        = $temp_date[2];
                                $tmp_hour        = $temp_time[0];
                                $tmp_minute        = $temp_time[1];
                                $tmp_second='00';
                                $tmp_today_info = $this->M_Tmp_year.'-'.$tmp_month.'-'.$tmp_day;
                                $date_time = $this->M_Tmp_year . '-' . $tmp_month . '-'.$tmp_day . ' ' . $tmp_hour . ':' . $tmp_minute . ':' .$tmp_second;
//                                echo "<pre>/n $this->M_Tmp_year .. $tmp_month .. $tmp_day .. $tmp_hour .. $tmp_minute .. $tmp_second /n";
                                $tmp_Unix_second=mktime($tmp_hour,$tmp_minute,$tmp_second,$tmp_month,$tmp_day,$this->M_Tmp_year);
                                $new_tmp_time = $tmp_Unix_second+43200;
                                $tmp_hour = date('H',$new_tmp_time);
                                $tmp_minute = date('i',$new_tmp_time);
                                $tmp_second = date('s',$new_tmp_time);
                                $tmp_month = date('m',$new_tmp_time);
                                $tmp_day = date('d',$new_tmp_time);
                                $tmp_Unix_second=mktime($tmp_hour,$tmp_minute,$tmp_second,$tmp_month,$tmp_day,$this->M_Tmp_year);
                                $this->M_Tmp_year = date('Y',$new_tmp_time);
                                $Unix_second=$tmp_Unix_second-$this->M_LostTime*60;
                                $close_time = date('Y-m-d H:i:s',$Unix_second);
                                $tmp_sj = $tmp_sj = $tmp_month.'-'.$tmp_day.'<br>'.$tmp_hour.':'.$tmp_minute.':'.$tmp_second;
                                $messages[6]=strtoupper($messages[6]);
                                $uptime=date("Y-m-d H:i:s");

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章