正则表达式

转自:http://hi.baidu.com/nemolfairy/blog/item/989c82155e377c05c93d6d55.html

//匹配单个字符c
Pattern pattern = Pattern.compile("c");
Matcher m = pattern.matcher("abcdef");


//匹配制表符
pattern = Pattern.compile("/t");
m = pattern.matcher("ab cdef");


//匹配回车符
pattern = Pattern.compile("/r");
m = pattern.matcher("ab/rcdef");


//匹配换行符
pattern = Pattern.compile("/n");
m = pattern.matcher("ab/ncdef");


//匹配/
pattern = Pattern.compile("
////");
m = pattern.matcher("ab//cdef");


//匹配$
pattern = Pattern.compile("
//$");
m = pattern.matcher("ab$$def");


//匹配^
pattern = Pattern.compile("//^");
m = pattern.matcher("ab^def^");


//匹配.
pattern = Pattern.compile("//.");
m = pattern.matcher("a.d.^");


//特别注意
//b//B,//b代表字与字之间的空隙,用来分割字符串的,那么//B就代表字符串之间没有隔断
pattern = Pattern.compile("
//D//W//S//b");
m = pattern.matcher("
a@45");


pattern = Pattern.compile("
//D//W//S//B");
m = pattern.matcher("
a@45");


//匹配任意字符,除了换行符
pattern = Pattern.compile(".");
m = pattern.matcher("a3455b/n8");


//匹配0-9之间的任一数字
pattern = Pattern.compile("[0-9]");
m = pattern.matcher("adsfa6gd");


//匹配除0-9之外的任意字符
pattern = Pattern.compile("[^0-9]");
m = pattern.matcher("adsfa6gd");


//匹配大小为3、由数字或字母或下划线组成的字符串
pattern = Pattern.compile("
//w{3}");
m = pattern.matcher("adsfa6gd");


//匹配数字或字母或下划线,最少3个,最多6个
pattern = Pattern.compile("
//w{3,6}");
m = pattern.matcher("adsfa6gd");


//匹配数字或字母或下划线,最少3个,没有上限
pattern = Pattern.compile("
//w{3,}");
m = pattern.matcher("adsfa6gd");


//?表示0个或1个
pattern = Pattern.compile("[^0-9f-kA-F]?");
m = pattern.matcher("adsfa6gd");


//+表示至少一个,相当于{1,}
pattern = Pattern.compile("a+b");
m = pattern.matcher("adsfaab");


//*表示0个或多个,相当于{0,}
pattern = Pattern.compile("a*b");
m = pattern.matcher("adsfaabb");


//^表示开始,$表示结束,即a开始b结束
pattern = Pattern.compile("^a+b$");
m = pattern.matcher("aaaaab");


//用
//b表示单词与空格之间的位置,即单词与单词的分界线
pattern = Pattern.compile(".//b.");
m = pattern.matcher("##&and");


//匹配bend,注意"
//bend//b"和"bend"的区别
pattern = Pattern.compile("
//bend//b");
m = pattern.matcher("weekend,endwith,end");

//|表示或的关系,匹配Tom或Jack
pattern = Pattern.compile("Tom|Jack");
m = pattern.matcher("Tom,Jack");


////s表示空格
pattern = Pattern.compile("(go//s*)+");
m = pattern.matcher("let go go go go!");


pattern = Pattern.compile("&(
//d+//.?//d*)");
m = pattern.matcher("&wer.ad,&23.");


//?表示非贪婪模式,尽可能不匹配思想
pattern = Pattern.compile("(d)(
//w+?)");
m = pattern.matcher("dxxxdxxxd");


pattern = Pattern.compile("(d)(
//w+?)(d)");
m = pattern.matcher("dxxxdxxxd");


//不加?表示贪婪模式,尽可能匹配
pattern = Pattern.compile("(d)(
//w+)");
m = pattern.matcher("dxxxdxxxd");


pattern = Pattern.compile("(d)(
//w+)(d)");
m = pattern.matcher("dxxxdxxxd");


pattern = Pattern.compile("<td>(.*)</td>");
m = pattern.matcher("<td><p>a</p></td>,<td><p>a</p></td>");


pattern = Pattern.compile("<td>(.*?)</td>");
m = pattern.matcher("<td><p>a</p></td>,<td><p>a</p></td>");


////1表示前面用到的第一个表达式,
//n表示前面第n个表达式
pattern = Pattern.compile("('|/")(.*?)//1");
m = pattern.matcher("'hello',/"world/"");


//区别"(
//w)//1{4,}"和"(//w){5,}"
pattern = Pattern.compile("(
//w)//1{4,}");
m = pattern.matcher("ccccc,ddddddd,wdddd");


pattern = Pattern.compile("(
//w){5,}");
m = pattern.matcher("ccccc,ddddddd,wdddd");


pattern = Pattern.compile("<(
//w+)//s*(//w+(=('|/").*?//4)?//s*)*>.*?<///1>");
m = pattern.matcher("<td id='td1' style='bgcolor:white'></td> ");


//(?=XXX)只是作为判断条件,只匹配Windows后面紧跟着NT或者XP的那个Windows,本身不占空间
pattern = Pattern.compile("Windows(?=NT|XP)");
m = pattern.matcher("Windows98,WindowsXP,Windows2009");


pattern = Pattern.compile("(
//w)((?=//1//1//1)(//1))+");
m = pattern.matcher("aaa 4444 55555 ffffff 7777777 88888888 999999999");


pattern = Pattern.compile("((?!//bstop//b).)+");
m = pattern.matcher("dfad dfasdfasd asdert etret stop ertwer");


//(?!XXX)相当于不等于XXX
pattern = Pattern.compile("do(?!//w)");
m = pattern.matcher("done,do,dog");


//(?<=XXX)是对左边进行判断
pattern = Pattern.compile("(?<=//d{4})//d+(?=//d{4})");
m = pattern.matcher("1234567890123456");


while(m.find()){
System.out.println(m.start());
System.out.println(m.group());
System.out.println(m.end());
}

你可能感兴趣的:(正则表达式)