正規表示式的基礎:
表示式 說明
/t 製表符.
/n 新行.
. 匹配任意字元.
| 匹配表示式左邊和右邊的字元. 例如, "ab|bc" 匹配 "ab" 或者 "bc".
匹配列表之中的任何單個字元. 例如, "[ab]" 匹配 "a" 或者 "b". "[0-9]" 匹配任意數字.
[^] 匹配列表之外的任何單個字元. 例如, "[^ab]" 匹配 "a" 和 "b" 以外的字元. "[^0-9]" 匹配任意非數字字元.
* 其左邊的字元被匹配任意次(0次,或者多次). 例如 "be*" 匹配 "b", "be" 或者 "bee".
+ 其左邊的字元被匹配至少一次(1次,或者多次). 例如 "be+" 匹配 "be" 或者 "bee" 但是不匹配 "b".
? 其左邊的字元被匹配0次或者1次. 例如 "be?" 匹配 "b" 或者 "be" 但是不匹配 "bee".
^ 其右邊的表示式被匹配在一行的開始. 例如 "^a" 僅僅匹配以 "a" 開頭的行.
$content$nbsp;其左邊的表示式被匹配在一行的結尾. 例如 "e$" 僅僅匹配以 "e" 結尾的行.
() 影響表示式匹配的順序,並且用作表示式的分組標記.
/ 轉義字元. 如果你要使用 "/" 本身, 則應該使用 "//".
/w 表示[a-z0-9]的字元
/w表示[^a-z0-9]的字元
/s表示[/t/n/r/f]一般就是乙個空格
詳見:http://hi.baidu.com/ismayday/blog/item/7f5b86942741d11dd21b708f.html
下面為乙個手機適配的例項:
private static string regex(string useragent) )(//d+)(.*)",
"$2$3$4$5");
if (ret != null)
/*** 根據ua返回結果字串為philips|samsung|sec-|mot|motorola-|asus|lenovo|lenovo|haier-|gionee($2)字母再帶大(小)寫字母$3,乙個或多個數字$4組成的字串
*/ret = regex(
useragent,
"(.*)(philips|samsung|sec-|mot|motorola-|asus|lenovo|lenovo|haier-|gionee)([a-z|a-z|//-|_|///]*)(//d+)(//d+)(.*)",
"$2$3$4");
if (ret != null)
ret = regex(
useragent,
"(.*)(panasonic|blackberry|bird|changhong|amoi|hedy|coolpad|yulong-coolpad|zte-|skyworth)([a-z|a-z|//-|_|///]*)(//d+)(//d+)(.*)",
"$2$3$4");
if (ret != null)
ret = regex(
useragent,
"(.*)(tianyu|konka|pantech|cect|tcl)([a-z|a-z|//-|_|///]*)(//d+)(//d+)(.*)",
"$2$3$4");
if (ret != null)
return null;
}public static string phone(string useragent)
if (isspider(useragent))
string ret;
string regex = "";
pattern p;
matcher m;
ret = regex(useragent);
if (ret != null)
/*** 乙個以上匹配大寫或小寫或帶-或帶/的字串+乙個以上的數字+乙個以上的非數字+0個或多個字元
*/regex = "([a-z|a-z|//-|_|///]+)(//d+)(//d+)(.*)";
p = pattern.compile(regex);
m = p.matcher(useragent);
if (m.find())
string ss = useragent.split("[//s,|///,|//*,]+");
if (ss[0].matches(".*(//d+).*"))
if (ss.length > 1)
return ss[0];
}
Java正規表示式
正規表示式結構簡介 字元 x 字元 x 反斜槓 0n 十進位制數 0 n 7 0nn 十進位制數 0nn 0 n 7 0mnn 十進位制數 0mnn 0 m 3,0 n 7 xhh 十六進製制數 0xhh uhhhh 十六進製制數 0xhhhh t 製表符 u0009 n 換行符 u000a r 回...
Java正規表示式
方便查詢的東西 基本語法 轉義字元 in d d d 數字0 9 多少到多少 d 非數字 0 9 非 w 單詞字元 a za z0 9 a3 w 非單詞字元 w s 空白 如 n t 0 1次 1 n次 0 n次 必須是n次 大於等於n次 n demo 中文 u0391 uffe5 英文 a za ...
Java正規表示式
舉例說明 the 開頭一定要有 the 字串 of despair 結尾一定要有 of despair 的字串 那麼,abc 就是要求以abc開頭和以abc結尾的字串,實際上是只有abc匹配。notice 匹配包含notice的字串。你可以看見如果你沒有用我們提到的兩個字元 最後乙個例子 就是說 模...