語法 | 意義 | 說明 |
"." | 任意字符 | |
"^" | 字符串開始 | '^hello'匹配'helloworld'而不匹配'aaaahellobbb' |
"$" | 字符串結尾 | 與上同理 |
"*" | 0 個或多個字符(貪婪匹配) | <*>匹配<title>chinaunix</title> |
"+" | 1 個或多個字符(貪婪匹配) | 與上同理 |
"?" | 0 個或多個字符(貪婪匹配) | 與上同理 |
*?,+?,?? | 以上三個取第一個匹配結果(非貪婪匹配) | <*>匹配<title> |
{m,n} | 對于前一個字符重復m到n次,{m}亦可 | a{6}匹配6個a、a{2,4}匹配2到4個a |
{m,n}? | 對于前一個字符重復m到n次,并取盡可能少 | ‘aaaaaa'中a{2,4}只會匹配2個 |
"http://" | 特殊字符轉義或者特殊序列 | |
[] | 表示一個字符集 | [0-9]、[a-z]、[A-Z]、[^0] |
"|" | 或 | A|B,或運算 |
(...) | 匹配括號中任意表達式 | |
(?#...) | 注釋,可忽略 | |
(?=...) | Matches if ... matches next, but doesn't consume the string. | '(?=test)' 在hellotest中匹配hello |
(?!...) | Matches if ... doesn't match next. | '(?!=test)' 若hello后面不為test,匹配hello |
(?<=...) | Matches if preceded by ... (must be fixed length). | '(?<=hello)test' 在hellotest中匹配test |
(?<!...) | Matches if not preceded by ... (must be fixed length). | '(?<!hello)test' 在hellotest中不匹配test |