PHP: 正規表達式
正規表達範例:
範例文件: hello.txt
he can't Hear the noises
made by the bear
near the pear tree in lobby
the rear garden since he bot
is deaf in one ear
<h1> is title 1
<h2 > is title 02
<h3> is title 003
<H5> is title 00005
<H6 id=red> is title 000006
title boot up
a quick brown fox jumps over the lazy dog.
background color is 0x25ABFF
<table><tr><td></table>
範例: 簡單匹配 尋找 "hear"
說明: grep 會將含有 "hear" 的行列出來,-n 選項同時顯示行數, -i 選項會忽略大小寫
[FreeBSD]/# grep -i -n 'hear' hello.txt
1:he can't Hear the noises
以下為特殊字元,如尋找以下字元,必須於字元加上 "\"
. 任意的字元都符合
^ 代表行首
$ 代表行尾
* 與其前方只佔一個位置的 RE 配合
[] 代表組合字元
\ 不解讀使特殊字元
[A-Z] 代表匹配範例 A-Z
[A-Za-z0-9] 代表匹配多個範例
[A-Z_] 代表匹配A-Z及底線_
[^A-Z] 代表匹配不包括A-Z
特殊代號
\w 等於 [A-Za-z0-9_]
\W 等於 [^A-Za-z0-9_], 代表匹配不包括 A-Za-z0-9_
\s 代表匹配 空白、\t\n\r\f
\S 代表匹配不包括 空白、\t\n\r\f
\d 等於 [0-9]
\D 等於 [^0-9], 代表不包括 0-9
範例: 簡單匹配 尋找 4-5字元
[FreeBSD]/# grep -n '\w{4,5}' hello.txt
範例: 簡單匹配 尋找 "g."
[FreeBSD]/# grep -n 'g\.' hello.txt
範例: 多組合匹配 [] 尋找 "bear" 或 "rear"
[FreeBSD]/# grep -n '[br]ear' hello.txt
2:made by the bear
4:the rear garden since he
說明: grep 會將含有 "bear" 或 "rear" 的行列出來
範例: 反相匹配 [^] 尋找 除了 "b" 及 "r"、跟著是 "ear"
[FreeBSD]/# grep -n '[^br]ear' hello.txt
1:he can't hear the noises
3:near the pear tree in
5:is deaf in one ear
範例: 模版匹配 . 尋找 其中3個字元,首為 "r" 及 尾為 "e"
[FreeBSD]/# grep -n 'r.e' hello.txt
3:near the pear tree in
4:the rear garden since he
說明: 利用 "." 代替一個任何字元
範例: 模版匹配 [][] 尋找 大小寫的 ,"<h1>"、"<h2>" .. "<h6>"
[FreeBSD]/# grep -n '<[Hh][123456]>' hello.txt
或 grep -n -i '<[h][1-6]>' hello.txt
6:<h1> is title 1
7:<h3> is title 3
8:<H5> is title 5
說明: 利用 "." 代替一個任何字元
範例: 模版匹配 ^ 尋找 行首為 "he"
[FreeBSD]/# grep -n '^he' hello.txt
1:he can't hear the noises
說明: 利用 "^" 尋找進行行首匹配
範例: 模版匹配 $ 尋找 行尾為 "ear"
[FreeBSD]/# grep -n 'ear$' hello.txt
2:made by the bear
5:is deaf in one ear
說明: 利用 "$" 尋找進行行尾匹配
範例: 模版匹配 ^$ 尋找 該行只有 "title"
[FreeBSD]/# grep -n '^title$' hello.txt
9:title
範例: 模版匹配 ^$ 尋找 該行完全不含字串
[FreeBSD]/# grep -n '^$' hello.txt
10:
12:
範例: 模版匹配 ^$ 尋找 該行只有5個字元
[FreeBSD]/# grep -n '^.....$' hello.txt
9:title
範例: 模版匹配 * 尋找 含有 a*b
[FreeBSD]/# grep -n 'a*b' hello.txt
2:made by the bear
11:a quick brown fox jumps over the lazy dog
範例: 模版匹配 * 尋找 含有HTML tag "<*>"
[FreeBSD]/# grep -n '<*>' hello.txt
範例: 模版匹配 . 尋找 含有 "<h.>"
[FreeBSD]/# grep -n '<h.>' hello.txt
6:<h1> is title 1
7:<h3> is title 3
範例: 模版匹配 * 尋找 含有 "<h*>"
[FreeBSD]/# grep -n '<h*>' hello.txt
說明: 此範例 不成功,沒有匹配
範例: 模版匹配 * 尋找 含有 "<h6*>"
[FreeBSD]/# grep -n '<h6*>' hello.txt
說明: 此範例 不成功,沒有匹配
範例: 模版匹配 尋找 含有 1位數字
[FreeBSD]/# grep -n '[0-9]' hello.txt
範例: 模版匹配 尋找 含有 2位數字
[FreeBSD]/# grep -n '[0-9][0-9]' hello.txt
範例: 模版匹配 尋找 含有 1位數字或其他
[FreeBSD]/# grep -n '[0-9]*' hello.txt
說明: 此範例無意義,會列出所有
範例: 模版匹配 尋找 含有 1位數 加上 1位數字或其他
[FreeBSD]/# grep -n '[0-9][0-9]*' hello.txt
說明: 此範例無意義,會列出最少含1位數字
範例: 模版匹配 尋找 16位顏色如 0x25ABFF
[FreeBSD]/# grep -in '0x[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' hello.txt
範例: 反相匹配,單字尋找 by,但不含 lobby
[FreeBSD]/# grep -in '[^a-z0-9]by[^a-z0-9]' hello.txt
[FreeBSD]/# grep -in '\bby\b' hello.txt // \b 嚴格匹配
範例: 延伸表示式 egrep ?,尋找可有可無 00 或 000
[FreeBSD]/# egrep -n '00?' hello.txt
8:<h3> is title 003
9:<H5> is title 00005
10:<H6 id=red> is title 000006
範例: 延伸表示式 egrep +,尋找組合+ bot 或 boot
[FreeBSD]/# egrep -n 'bo+t' hello.txt
grep -n 'bo*t' hello.txt
範例: 延伸表示式 egrep |,尋找 bear 或 tree 或 one
[FreeBSD]/# egrep -n 'bear|tree|one' hello.txt
2:made by the bear
3:near the pear tree in lobby
5:is deaf in one ear
範例: 延伸表示式 egrep (),尋找分組 company 或 companies
[FreeBSD]/# egrep -n 'compan(y|ies)' hello.txt
範例: 延伸表示式 egrep 複式,尋找分組 bana 或 banana
[FreeBSD]/# egrep -n 'b(an)+a' hello.txt
sed 範例
========
範例文件:text.txt
I have big cat, you have big dog.
The cat is gray.
It indicates that cat is too fat.
my tel no is 123456
this is space!
today is 12-30-2011 Sat
tomorrow is 31/12/2011 Sat
101 1001 10001 100001 1000001
note: <#IMG=file/abc_001_xyz.gif width=150><BR>YESTERDAY=2011-12-29 ok?
範例: 單字轉換 尋找 'cat' 轉為 'tiger'
說明: 此範例將單字轉成另外一組,選項 -e 設定轉換,
[FreeBSD]/# sed -e 's/cat/tiger/' text.txt
範例:將結果顯示到 標準輸出 或檔案
[FreeBSD]/# sed -e 's/cat/tiger/' text.txt > tmp; mv tmp text.txt
範例: 單字轉換 尋找 'big' 轉為 'small'
[FreeBSD]/# sed -e 's/big/small/g' text.txt
說明: 預設將每行單字轉成另外一組1次,加上 'g' (global) 能轉換所有字
範例: 加入語法,每行前加入 '==>'
[FreeBSD]/# sed -e 's/^/==>/' text.txt
範例: 加入語法,每行前加入 '{=='
sed -e 's/&/{==/' text.txt
範例: 單字轉換,尋找 'cat' 轉為 'tiger'
[FreeBSD]/# sed -e 's/ cat / tiger /' text.txt
說明: 此語法不能解決 cat, cat. (cat)等
範例: 重置單字,將 數字 轉為 '{數字}'
[FreeBSD]/# sed -e 's/[0-9]/{&}/' text.txt // 錯誤:只有第一個數字
[FreeBSD]/# sed -e 's/[0-9]*/{&}/' text.txt // 錯誤:只會置於行首
[FreeBSD]/# sed -e 's/[0-9][0-9]*/{&}/' text.txt // 正確語法
範例: 加入語法,每行前加入 ' '
[FreeBSD]/# sed -e 's/^/ /' text.txt
範例: 刪減語法,刪減行首空格
[FreeBSD]/# sed -e 's/ *//' text.txt // 語法錯誤,但結果正確:將零個以上的空格轉換成 ''
[FreeBSD]/# sed -e 's/ *//' text.txt // 錯誤:將1個以上的空格轉換成 ''
[FreeBSD]/# sed -e 's/^ *//' text.txt // 正確:將行首,1個以上的空格轉換成 ''
範例: 刪除行,刪除含 fat 的行
[FreeBSD]/# sed -e '/fat/d' text.txt
範例: 刪除行,刪除空行
[FreeBSD]/# sed -e '/^$/d' text.txt
範例: 轉換語法,日期格式
[FreeBSD]/# sed -e 's/\([01][0-9]\)-\([0-3][0-9]\)-\([0-9][0-9]*\)/\3-\1-\2/' text.txt //將 m-d-y 轉成 y-m-d
[FreeBSD]/# sed -e 's/\([0-3][0-9]\)\/\([01][0-9]\)\/\([0-9][0-9]*\)/\3-\2-\1/' text.txt //將 d/m/y 轉成 y-m-d
範例: 轉換範圍,指定行數
[FreeBSD]/# sed -e '2s/cat/tiger/g' text.txt // 只轉換第2行
[FreeBSD]/# sed -e '$s/cat/tiger/g' text.txt // 只轉換最後一行
[FreeBSD]/# sed -e '3,$s/cat/tiger/g' text.txt // 只轉換第3行 至 最後一行
範例: 範圍轉換,指定匹配
[FreeBSD]/# sed -e '/fat/s/cat/tiger/g' text.txt // 只轉換含 fat 行
[FreeBSD]/# sed -e '/fat/{ // 另一種易讀的寫法
s/cat/tiger/g
}' text.txt
[FreeBSD]/# sed -e '2,/fat/s/cat/tiger/g' text.txt // 只轉換第2行至 含fat行
範例: 重複匹配,尋找 字串是1之後接著數個0,轉換加上{}
[FreeBSD]/# sed -e 's/10{2,4}1/{&}/g' text.txt
範例: 把第10個字元起刪除
[FreeBSD]/# sed -e 's/&(.\{9\}).*$/\1/' text.txt
範例: 匹配抽起、轉換,將 <#IMG=file/abc_xyz.gif> 轉換成 <IMG SRC=file/abc_xyz.gif>
[FreeBSD]/# sed -e 's/<#IMG=\([a-z0-9_\/\.]*\)>/<IMG SRC=\1>/' text.txt
電郵測試 OK
$pattern='/^([\w|\.]+\w+\@\w+\.[\w|\.]+)$/i';
$email='a_bc.lmn@jkl.opq.com.hk';$match=preg_match($pattern,$email)?'YES':'NO';echo "EMAIL:$email PATTERN:$pattern MATCH:$match, ";
測試 日期格式
$pattern='/(\d{4})-(\d{2})-(\d{2})/';
$day='1999-12-31';
$match=(preg_match($pattern,$day,$val))?'YES':'NO';
echo "DAY:$day PATTERN:$pattern MATCH:$match $val[0] $val[1],$val[2],$val[3] ";
測試香港身分證 Hong Kong ID Card
preg_match('/^[A-Z]\d{6}\([0-9A]\)$/','A123456(7)')
$d1=preg_replace("/(\d{4})-(\d{2})-(\d{2})/","\\3/\\2\\1","2011-12-31"); // 抽取年月日
$contents=preg_replace("/\s+/", " ", $contents); //過濾多餘回車
$contents=preg_replace("/<[ ]+/si","<",$contents); //過濾<__("<"號後面帶空格)
$contents=preg_replace("/<\!--.*?-->/si","",$contents); //註釋
$contents=preg_replace("/<(\!.*?)>/si","",$contents); //過濾DOCTYPE
$contents=preg_replace("/<(\/?html.*?)>/si","",$contents); //過濾html標籤
$contents=preg_replace("/<(\/?meta.*?)>/si","",$contents); //過濾meta標籤
$contents=preg_replace("/<(\/?body.*?)>/si","",$contents); //過濾body標籤
$contents=preg_replace("/<(\/?link.*?)>/si","",$contents); //過濾link標籤
$contents=preg_replace("/<(\/?form.*?)>/si","",$contents); //過濾form標籤
$contents=preg_replace("/cookie/si","COOKIE",$contents); //過濾COOKIE標籤
$contents=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$contents); //過濾applet標籤
$contents=preg_replace("/<(\/?applet.*?)>/si","",$contents); //過濾applet標籤
$contents=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$contents); //過濾style標籤
$contents=preg_replace("/<(\/?style.*?)>/si","",$contents); //過濾style標籤
$contents=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$contents); //過濾title標籤
$contents=preg_replace("/<(\/?title.*?)>/si","",$contents); //過濾title標籤
$contents=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$contents); //過濾object標籤
$contents=preg_replace("/<(\/?objec.*?)>/si","",$contents); //過濾object標籤
$contents=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$contents); //過濾noframes標籤
$contents=preg_replace("/<(\/?noframes.*?)>/si","",$contents); //過濾noframes標籤
$contents=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$contents); //過濾frame標籤
$contents=preg_replace("/<(\/?i?frame.*?)>/si","",$contents); //過濾frame標籤
$contents=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$contents); //過濾script標籤
$contents=preg_replace("/<(\/?script.*?)>/si","",$contents); //過濾script標籤
$contents=preg_replace("/javascript/si","Javascript",$contents); //過濾script標籤
$contents=preg_replace("/vbscript/si","Vbscript",$contents); //過濾script標籤
$contents=preg_replace("/on([az]+)\s*=/si","On\\1=",$contents); //過濾script標籤
$contents=preg_replace("//si","",$contents); //過濾script標籤,如javAsCript:alert('aabb)
$contents=preg_replace("/<(\/?table.*?)>/si","",$contents); //過濾table標籤
$contents=preg_replace("/<(\/?tr.*?)>/si","",$contents); //過濾tr標籤
$contents=preg_replace("/<(\/?tbody.*?)>/si","",$contents); //過濾tbody標籤
$contents=preg_replace("/<(\/?td.*?)>/si",",",$contents); //過濾td標籤
$contents=preg_replace("/<(\/?img.*?)>/si","",$contents); //過濾img標籤
$contents=preg_replace("/<(\/?input.*?)>/si","",$contents); //過濾input標籤
$contents=preg_replace("/<(\/?font.*?)>/si","",$contents); //過濾font標籤
$contents=preg_replace("/<(\/?th.*?)>/si","",$contents); //過濾th標籤
$contents=preg_replace("/<(head.*?)>(.*?)<(\/head.*?)>/si","",$contents); //過濾head標籤
$contents=preg_replace("/<(select.*?)>(.*?)<(\/select.*?)>/si","",$contents); //過濾select標籤
$contents=preg_replace("/<(strong.*?)>(.*?)<(\/strong.*?)>/si","",$contents); //過濾strong標籤
$contents=preg_replace("/<(label.*?)>(.*?)<(\/label.*?)>/si","",$contents); //過濾label標籤
$contents=preg_replace("/<(a.*?)>(.*?)<(\/a.*?)>/si","",$contents); //過濾a標籤
【參考來源】http://www.rtfiber.com.tw/~changyj/regex.1/index.html
附加檔案:0 | 讀取:173 | 留言:0