import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternTest {
public static void main(String[] args) {
//Java中用正则表达式截取字符串中第一个出现的英文左括号之前的字符串--- 北京市
String text = "北京市(海淀区)(朝阳区)(西城区)";
Pattern pattern = Pattern.compile(".*?(?=()" );
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println(matcher.group(0));
}
//查找以Java开头,任意结尾的字符串 -- true
Pattern pattern = Pattern.compile("^Java.*");
Matcher matcher = pattern.matcher("Java不是人");
boolean b= matcher.matches();
//当条件满足时,将返回true,否则返回false
System.out.println(b);
//以多条件分割字符串时
/*Java
Hello
World
Java
Hello
World
Sun*/
Pattern pattern = Pattern.compile("[,|]+");
String[] strs = pattern.split("Java Hello World Java,Hello,World|Sun");
for (int i=0;i<strs.length;i++) {
System.out.println(strs[i]);
}
//文字替换(首次出现字符)--Java Hello World,正则表达式 Hello World
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
//替换第一个符合正则的数据
System.out.println(matcher.replaceFirst("Java"));
//文字替换(全部)---Java Hello World,Java Hello World
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
//替换第一个符合正则的数据
System.out.println(matcher.replaceAll("Java"));
//文字替换(置换字符) ---Java Hello World,正则表达式 Hello World ");
StringBuffer sbr = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sbr,"Java");
}
matcher.appendTail(sbr);
System.out.println(sbr.toString());
//验证是否为邮箱地址 ---true
String str="ceponline@yahoo.com.cn";
Pattern pattern = Pattern.compile("[w.-]+@([w-]+.)+[w-]+",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.matches());
//截取http://地址 ---http://dsds/r/n
Pattern pattern = Pattern.compile("(http://|https://){1}[w.-:]+");
Matcher matcher = pattern.matcher("dsdsds<fdf" rel="nofollow">http://dsds//gfgffdfd>fdf");
StringBuffer buffer = new StringBuffer();
while(matcher.find()){
buffer.append(matcher.group());
buffer.append("/r/n");
System.out.println(buffer.toString());
}
//去除html标记 ---主页
Pattern pattern = Pattern.compile("<.+?>",Pattern.DOTALL);
Matcher matcher = pattern.matcher("<a href="index.html">主页</a>");
String string = matcher.replaceAll("");
System.out.println(string);
//查找html中对应条件字符串 ---index.html
Pattern pattern = Pattern.compile("href="(.+?)"");
Matcher matcher = pattern.matcher("<a href="index.html">主页</a>");
if(matcher.find()) {
System.out.println(matcher.group(1));
}
//matcher.group的使用
/*Group 0:World!
Group 1:or
Group 2:ld!
Start 0:6 End 0:12
Start 1:7 End 1:9
Start 2:9 End 2:12
Wor*/
String str = "Hello,World! in Java.";
Pattern pattern = Pattern.compile("W(or)(ld!)");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println("Group 0:"+matcher.group(0));//得到第0组——整个匹配
System.out.println("Group 1:"+matcher.group(1));//得到第一组匹配——与(or)匹配的
System.out.println("Group 2:"+matcher.group(2));//得到第二组匹配——与(ld!)匹配的,组也就是子表达式
System.out.println("Start 0:"+matcher.start(0)+" End 0:"+matcher.end(0));//总匹配的索引
System.out.println("Start 1:"+matcher.start(1)+" End 1:"+matcher.end(1));//第一组匹配的索引
System.out.println("Start 2:"+matcher.start(2)+" End 2:"+matcher.end(2));//第二组匹配的索引
System.out.println(str.substring(matcher.start(0),matcher.end(1)));//从总匹配开始索引到第1组匹配的结束索引之间子串——Wor
}
//替换指定{}中文字 --Java目前的发展史是由1995年-2007年
String str = "Java目前的发展史是由{0}年-{1}年";
String[][] object={new String[]{"{0}","1995"},new String[]{"{1}","2007"}};
System.out.println(replace(str,object));
//匹配连续的相同字符
String reg = "(.)1";
String str = "aa";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println("ok");
}
}
public static String replace(final String sourceString,Object[] object) {
String temp=sourceString;
for(int i=0;i<object.length;i++){
String[] result=(String[])object[i];
Pattern pattern = Pattern.compile(result[0]);
Matcher matcher = pattern.matcher(temp);
temp=matcher.replaceAll(result[1]);
}
return temp;
}
}