package com.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternTest {
private static void test0() {
String str = "<biao><>c<b>x";
Pattern pattern;
Matcher matcher;
// 贪婪: 最长匹配 .* : 输出: <biao><>c<b>
pattern = Pattern.compile("<.*>");
matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
// 不知是否非贪婪 .*? : 输出: <biao>, <>, <b>
pattern = Pattern.compile("<.*?>");
matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
// 使用组, 输出<>里的内容, 输出: 'biao', ' ', 'b'
// 0组代表整个表达式, 子组从1开始
pattern = Pattern.compile("<(.*?)>");
matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(">>"+matcher.group(1));
}
}
/** 查找以Java开头的任意字符串 */
public static void test1() {
Pattern pattern = Pattern.compile("^Java.*");
Matcher m = pattern.matcher("Java哈哈");
System.out.println(m.find());
}
/** 以多条件分割字符串 */
public static void test2() {
Pattern pattern = Pattern.compile("[,|\\+]+");
String str[] = pattern.split("Java+ Hello World Java,Hello,,World|Sun");
if(str != null) {
for(String s : str) {
System.out.println(">> " +s);
}
}
}
/** 文字替换 */
public static void test3() {
Pattern pattern = Pattern.compile("abc*");
Matcher matcher = pattern.matcher("xabcccc测试abd, abcce");
String str = matcher.replaceAll("(?)");
System.out.println(str);
}
/** 文字替换(替换字符) 结果将会是 x(?)测试(?)d, (?) */
public static void test4() {
Pattern pattern = Pattern.compile("123|4(567)");
Matcher matcher = pattern.matcher("0123456789abcde");
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
System.out.println(">>"+ matcher.start()+":"+matcher.end());
matcher.appendReplacement(sb, "(?)");
}
matcher.appendTail(sb);
System.out.println(sb);
}
public static void test5() {
String regEx="B(ond)";
String str="My name is Bond. James Bond.";
Pattern p=Pattern.compile(regEx);
Matcher matcher=p.matcher(str);
if(matcher.find()){
System.out.println(matcher.groupCount());
System.out.println(matcher.start());
String group_0 = matcher.group(0);
String group_1 = matcher.group(1);
System.out.println("Group 0 " + group_0);
System.out.println("Group 1 " + group_1);
System.out.println(str);
}
}
// 验证是否为邮箱地址
public static void test6() {
String str="ponline@yahoo.com.cn.jcWe--_*";
Pattern pattern =
Pattern.compile("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+");
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.matches());
}
/** 去除html 标记 */
public static void test7() {
Pattern pattern = Pattern.compile(".+?");
Matcher matcher = pattern.matcher("<a 测试 href=\"index.html\">主页</a>");
matcher.find();
System.out.println(matcher.group());
String string = matcher.replaceFirst("");
System.out.println(string);
}
/** 查找html 中对应条件字符串 **/
public static void test8() {
Pattern pattern = Pattern.compile("href=\"(.+?)\"");
Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");
if(matcher.find())
System.out.println(matcher.group(1));
}
/** 截取http://地址 **/
public static void test9() {
Pattern pattern = Pattern.compile("(http://|https://){1}.+");
Matcher matcher = pattern.matcher("dsdsds<http://dfhf.\\sd-: s//国家gfg-ffdfd>fdf");
StringBuffer buffer = new StringBuffer();
while(matcher.find()){
buffer.append(matcher.group());
buffer.append("\r\n");
System.out.println(buffer.toString());
}
}
/** 替换指定{}中文字 **/
public static void test10() {
String str = "Java目前的发展史是由{0}年-{1}年";
String[][] object={new String[]{"\\{0\\}","1995"},new String[]{"\\{1\\}","2007"}};
System.out.println(replace(str,object));
}
public static String replace(String sourceString, String[][] 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;
}
public static void main(String[] args) throws Exception{
PatternTest.test10();
}
}
</script>