總網頁瀏覽量

咕狗大神

2012年8月18日 星期六

Regular Expression

摘錄 wikipedia 正規(常規)表示式: http://zh.wikipedia.org/wiki/Regular_Expression

最近剛好要用到文字的 parsing ,所以複習一下,也順便作個筆記,以免再度失憶。

幾個比較常用的如下:


符號 意義 範例
. 任意字元 regex =@"^thank."; regex.match("thank u"); regex.match("thankx");
? 出現零次或一次 regex =@"^thankx?"; regex.match("thank u"); regex.match("thankx");
* 重複出現任意次數(含不出現) regex =@"^thankx*"; regex.match("thank u"); regex.match("thankxxxxxx");
+ 至少重複出現一次 regex =@"^thankx+"; regex.match("thankx"); regex.match("thankx");
{n} 必須重複出現n次 regex =@"^thankx{3}"; regex.match("thankxxx");
{n,} 至少重複出現n次 regex =@"^thankx{3,}"; regex.match("thankxxx"); regex.match("thankxxxxxx");
{n,m} 至少重複出現n次,但不超過m次 regex =@"^thankx{3, 5}"; regex.match("thankxxx"); regex.match("thankxxxxx");
[^...] 中括弧裡面的字元以外 regex =@"^th[^iou]nk"; regex.match("thank"); regex.match("thenk");
其他的有用到再寫。