/**
* 查找是否包含指定的字符串
* @return
*/
public static boolean find(){
String str = "abc efg ABC";
String regex = "a|f";//e或者f
Pattern pattern = Pattern.compile(regex);
Matcher mat = pattern.matcher(str);
boolean flag = mat.find();
//仅使用一次的正则表达式可以如下使用
// boolean f = Pattern.matches( "a|f","abc efg ABC");
return flag;
}
/**
* 提取字符串
* @return
*/
public static String get()
{
String regEx=".+/(.+)$";
String str="x/tt.x";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
boolean rs=m.find();
System.out.println(rs);
String s = null;
for(int i=1;i<=m.groupCount();i++){
s = m.group(i);
}
return s;
}
/**
* 分隔字符串
*/
public static void split(){
String regex = ",";
String str = "a,g,c,d";
Pattern p = Pattern.compile(regex);
String[] ary = p.split(str);
for (int i = 0; i < ary.length; i++) {
System.out.println(ary[i]);
}
}
/**
* 替换或删除
* @return
*/
public static String replace(){
String regex="a+";
String str = "asdfadfasd";
Pattern p = Pattern.compile(regex);
Matcher mat = p.matcher(str);
String s = mat.replaceAll("A");
// String s = mat.replaceAll("");//替换成空的话,删除匹配的值
return s;
}
* 查找是否包含指定的字符串
* @return
*/
public static boolean find(){
String str = "abc efg ABC";
String regex = "a|f";//e或者f
Pattern pattern = Pattern.compile(regex);
Matcher mat = pattern.matcher(str);
boolean flag = mat.find();
//仅使用一次的正则表达式可以如下使用
// boolean f = Pattern.matches( "a|f","abc efg ABC");
return flag;
}
/**
* 提取字符串
* @return
*/
public static String get()
{
String regEx=".+/(.+)$";
String str="x/tt.x";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
boolean rs=m.find();
System.out.println(rs);
String s = null;
for(int i=1;i<=m.groupCount();i++){
s = m.group(i);
}
return s;
}
/**
* 分隔字符串
*/
public static void split(){
String regex = ",";
String str = "a,g,c,d";
Pattern p = Pattern.compile(regex);
String[] ary = p.split(str);
for (int i = 0; i < ary.length; i++) {
System.out.println(ary[i]);
}
}
/**
* 替换或删除
* @return
*/
public static String replace(){
String regex="a+";
String str = "asdfadfasd";
Pattern p = Pattern.compile(regex);
Matcher mat = p.matcher(str);
String s = mat.replaceAll("A");
// String s = mat.replaceAll("");//替换成空的话,删除匹配的值
return s;
}