java的正则表达式,功能还是挺强的。下面通过代码说明。
package com.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="PS{d86656E5-82c2-4257-8b52-95a15b18bdcd},TESTabc={6X},PPID={/yid=mouse at/ataddr=12.24.0.5},thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd";
String regex="(?<=\\{)(.*)(?=\\}";
String gegexL[]= {
"(?<=\\{)(.*)(?=\\})",
"\\{(.*)\\}",
"\\{([^,]*)\\}",
"TEST([a-z|A-Z]*)=\\{([^,]*)\\}",
"TEST([a-z|A-Z]*)\\=\\{([^,]*)\\}",
"TEST([a-z|A-Z]*)\\=\\{([^,]*)\\,",
"TEST([a-z|A-Z]*)\\=\\{([a-z|A-Z|0-9]*)\\}\\,"
};
for(int i=0;i<gegexL.length;i++){
String rs=regexReplace(str,gegexL[i]);
System.out.println("regexReaplace: "+rs);
}
String str1="我...我...要...要...吃...吃...饭";
String regext ="\\.+";
String newStr ="";
str=regexNewString(str1,regext,newStr);
regext ="(.)\\1+";
newStr= "$1";
regexNewString(str1,regext,newStr);
str="foo[x+13,y-2,10]";
regext = "foo\\{([^,]*),([^,]*),([^,]*)\\]";
newStr = "foo($2,$1,$3)";
regexNewString(str1,regext,newStr);
String gegex2L[]={
"foo(10,7,2)",
"foo(x+10,y-7,2)",
"foo(bar(10),x+y+7,5)"
};
regext = "foo\\(([^,]*),([^,]*),([^,]*)\\)";
for(int i=0;i<gegex2L.length; i++){
regexNewString(gegex2L[i],regext,newStr);
}
str="TH{d86656E5-82c2-4257-8b52-95a15b18bdcd},YI={8138a35c-82c2-4257-8b52-95a15b18bdcd}";
regext="TH\\{([^,]*)\\},YI=\\{([^,]*)\\}";
newStr="TH{$1},Yhrt={$2}";
regexNewString(str,regext,newStr);
str="zhang 2000Windows xinping !";
regext="(?<=95|98|NT|2000)Windows";
match(str,regext);
regext="(?!Windows)";
regext="^(?:(?!(Windoes|def)).)*$";
match(str,regext);
str="zhang [email protected] test";
regex="\\w+@(163|gmail|sina|qq)\\.\\w+";
match(str,regext);
System.out.println(" --------: ");
str="ABhedeCD";
regext="((?!hede).)*";
match(str,regext);
str="hi,hello,hzy,how nice it is";
regext="^(?!.*hello.*hzy)";
match(str,regext);
regexReplace(str,regext);
str="abba, anallagmatic";
regext="^(.*(.)(.)$2$1)";
regexReplace(str,regext);
str="4632";
regext="[0-9]*";
match(str,regext);
}
public static boolean match(String str,String reg){
boolean res=false;
Matcher mat=Pattern.compile(reg).matcher(str);
String fdsel="";
if(mat.find()){
fdsel=mat.group();
res=true;
}
System.out.println(" matchString: "+fdsel);
return res;
}
public static String regexReplace(String str,String reg){
Matcher mat=Pattern.compile(reg).matcher(str);
String fdsel="";
if(mat.find()){
fdsel=mat.group();
}
System.out.println(" SubString: "+fdsel);
return str.replaceFirst(reg, "");
}
public static String regexNewString(String str,String reg,String newStr){
String str2=str.replaceAll(reg, newStr);
System.out.println(" regexNewString: "+str2);
return str2;
}
}
运行结果如下 ,除了普通的正则匹配, $n的支持还是挺强的。
SubString: d86656E5-82c2-4257-8b52-95a15b18bdcd},TESTabc={6X},PPID={/yid=mouse at/ataddr=12.24.0.5
regexReaplace: PS{},thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd
SubString: {d86656E5-82c2-4257-8b52-95a15b18bdcd},TESTabc={6X},PPID={/yid=mouse at/ataddr=12.24.0.5}
regexReaplace: PS,thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd
SubString: {d86656E5-82c2-4257-8b52-95a15b18bdcd}
regexReaplace: PS,TESTabc={6X},PPID={/yid=mouse at/ataddr=12.24.0.5},thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd
SubString: TESTabc={6X}
regexReaplace: PS{d86656E5-82c2-4257-8b52-95a15b18bdcd},,PPID={/yid=mouse at/ataddr=12.24.0.5},thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd
SubString: TESTabc={6X}
regexReaplace: PS{d86656E5-82c2-4257-8b52-95a15b18bdcd},,PPID={/yid=mouse at/ataddr=12.24.0.5},thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd
SubString: TESTabc={6X},
regexReaplace: PS{d86656E5-82c2-4257-8b52-95a15b18bdcd},PPID={/yid=mouse at/ataddr=12.24.0.5},thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd
SubString: TESTabc={6X},
regexReaplace: PS{d86656E5-82c2-4257-8b52-95a15b18bdcd},PPID={/yid=mouse at/ataddr=12.24.0.5},thisid=8138a35c-82c2-4257-8b52-95a15b18bdcd
regexNewString: 我我要要吃吃饭
regexNewString: 我.我.要.要.吃.吃.饭
regexNewString: 我...我...要...要...吃...吃...饭
regexNewString: foo(7,10,2)
regexNewString: foo(y-7,x+10,2)
regexNewString: foo(x+y+7,bar(10),5)
regexNewString: TH{d86656E5-82c2-4257-8b52-95a15b18bdcd},Yhrt={8138a35c-82c2-4257-8b52-95a15b18bdcd}
matchString: Windows
matchString: zhang 2000Windows xinping !
matchString: zhang [email protected] test
--------:
matchString: AB
matchString:
SubString:
SubString:
matchString: 4632