标签:
正则表达式语法结构图:

Java正则表达式类库结构图:

Java典型例子
1、String类 matches()方法
判断字符串是否符合特定正则表达式
@Test public void testRegex(){
String regex = ".*\\d{3}.*";
String str1 = "11tec34";
String str2 = "285dffd";
String str3 = "bac7736db";
//输出:false, true, true
System.out.println(str1.matches(regex) + "," + str2.matches(regex) + "," + str3.matches(regex));
}
2、String类 split()方法
用正则表达式分割字符串
@Test public void testRegex2(){
String input = "hello3world8my9name1is5jacket";
String regex = "\\d"; //以数字作为分隔符
String strs[] = input.split(regex);
//输出:hello world my name is jacket
for(String str : strs){
System.out.print(str + " ");
}
System.out.println();
input = "hellodogworlddogmydognamedogisdogjacket";
regex = "dog"; //以"dog"字符作为分隔符
strs = input.split(regex);
//输出:hello world my name is jacket
for(String str : strs){
System.out.print(str + " ");
}
}
3、String类 replace()方法
用正则表达式替换字符
@Test public void testRegex3(){
String input = "hello3world8my9name1is5jacket";
String regex = "\\d"; //以数字作为分隔符
String firstStr = input.replaceFirst(regex, ":"); //只替换第一个匹配的字符
String allStr = input.replaceAll(regex, ":"); //替换所有匹配的字符
//输出:
//hello:world8my9name1is5jacket
//hello:world:my:name:is:jacket
System.out.println(firstStr);
System.out.println(allStr);
}
4、Pattern.matches() 静态方法
判断字符串是否符合正则表达式,等同于String类的matches()方法
@Test public void testRegex4(){
String regex1 = "\\bdog\\b";
String regex2 = ".*\\bdog\\b.*";
String input = "dog dog dog doggie dogg";
//输出:false,true
System.out.println(Pattern.matches(regex1, input) + "," + Pattern.matches(regex2, input));
}
5、Pattern类 split()方法
根据正则表达式分割字符串,等同于String类的split()方法
//6.split() 分割字符串 -> 等同于String的split()方法
@Test public void testRegex6(){
String regex = "\\d";
String input = "hello3world5hello8java";
Pattern p = Pattern.compile(regex);
String strs[] = p.split(input);
//输出:hello world hello java
for(String str : strs){
System.out.print(str + " ");
}
}
6、Pattern.quote() 静态方法
获取匹配此字符串的正则表达式
@Test public void testRegex6(){
//例子1
String input = ".";
String regex = Pattern.quote(input);
boolean flag = Pattern.matches(regex, input);
//输出:\Q353434\E true
System.out.println(regex);
System.out.println(flag);
//例子2
input = "test.test";
String errorOutput = input.replaceAll(".", "/");
//输出://///////
System.out.println(errorOutput);
//例子3
String correctOutput = input.replaceAll(Pattern.quote("."), "/");
//输出:test/test
System.out.println(correctOutput);
}
7、Matcher类 find()方法,start()方法,end()方法
@Test public void testRegex7(){
//m.start() m.end()
String regex = "\\bdog\\b";
String input = "dog dog dog doggie dogg";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input); // 获得匹配器对象
int count = 0;
while (m.find()) {
count++;
//Match number 1,start(): 0,end(): 3
//Match number 2,start(): 4,end(): 7
//Match number 3,start(): 8,end(): 11
System.out.print("Match number " + count);
System.out.print(",start(): " + m.start());
System.out.println(",end(): " + m.end());
}
}
8、Matcher类 find(int index)方法,start(int index),end(int index)
@Test public void testRegex8(){
//m.start() m.end()
String regex = "\\bdog\\b";
String input = "dog dog dog doggie dogg";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input); // 获得匹配器对象
//m.find(int start)
//输出:start(): 0,end(): 3
//start(): 4,end(): 7
m.find(0); //从要匹配的字符串的第几个字符开始寻找
System.out.print("start(): " + m.start());
System.out.println("end(): " + m.end());
m.find();
System.out.print("start(): " + m.start());
System.out.println("end(): " + m.end());
//m.find(int start)
//输出:start(): 4,end(): 7
//start(): 8,end(): 11
m.find(4);
System.out.print("start(): " + m.start());
System.out.println("end(): " + m.end());
m.find();
System.out.print("start(): " + m.start());
System.out.println("end(): " + m.end());
//m.start(int group) m.end(int group)
m.find(0); //
int firstStartGroupIndex = m.start(0);
System.out.println(firstStartGroupIndex);
}
9、Matcher类 lookingAt()方法,matches()方法
判断字符串是否匹配特定的正则表达式,lookingAt()方法不要求全部匹配,而matches()方法要求全部匹配
@Test public void testRegex9(){
String regex = "foo";
String str = "fooooooooooooooooo";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
System.out.println("Current regex is: " + regex);
System.out.println("Current str is: " + str);
System.out.println("lookingAt(): " + matcher.lookingAt()); //true
System.out.println("matches(): " + matcher.matches()); //false,因为matches要求全部匹配
}
此外还有其他的Matcher类的方法未整理,
//还有Matcher类的其他方法未整理:
//1.group(int group)方法
//2.appendReplacement() / appendTail() / replaceAll() / replaceFirst() / quoteReplacement()
//99.Matcher类的find()方法,start()方法,end()方法
更多详细的正则表达式博文可以参考:
微软官方文档:https://msdn.microsoft.com/zh-cn/library/ae5bf541(v=vs.80).aspx
一个很好的正则表达式教程:http://www.java3z.com/cwbwebhome/article/article8/Regex/Java.Regex.Tutorial.html#reg7
标签:
原文地址:http://www.cnblogs.com/chanshuyi/p/4387528.html