将String中
{ban,2000000345,468,60,ban}替换成200000034546860 public static void main(String[] args) {
String regexBan="\\{ban,([0-9]+),([0-9]+),([0-9]+),ban\\}";
String emailContent ="test banner <br> <br>" +
"banner :{ban,2000000276,728,90,ban} <br> <br>banner 2 :{ban,3000000344,728,90,ban}<br>" +
"banner 3:{ban,2000000345,468,60,ban}<br>banner 4:{ban,3000000369,120,600,ban}<br>";
String matchRegex= ".*" + regexBan + ".*" ;
Pattern p = Pattern.compile(matchRegex,Pattern.DOTALL );
int cnt = 0;
while(true){
Matcher match = p.matcher(emailContent);
System.out.println("matches:"+match.matches());
if(match.matches()){
cnt++;
String Id=match.replaceFirst("$1");
String width=match.replaceFirst("$2");
String height=match.replaceFirst("$3");
System.out.println("para:"+Id+":"+width+":"+height);
emailContent=emailContent.replaceFirst(regexBan,Id + width + height);
System.out.println("email content:" + emailContent );
}else break;
}
}结果:
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :{ban,3000000344,728,90,ban}<br>banner 3:{ban,2000000345,468,60,ban}<br>banner 4:{ban,3000000369,120,600,ban}<br>
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :3000000369120600<br>banner 3:{ban,2000000345,468,60,ban}<br>banner 4:{ban,3000000369,120,600,ban}<br>
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :3000000369120600<br>banner 3:3000000369120600<br>banner 4:{ban,3000000369,120,600,ban}<br>
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :3000000369120600<br>banner 3:3000000369120600<br>banner 4:3000000369120600<br>
matches:false
{ban,2000000345,468,60,ban}替换成200000034546860 public static void main(String[] args) {
String regexBan="\\{ban,([0-9]+),([0-9]+),([0-9]+),ban\\}";
String emailContent ="test banner <br> <br>" +
"banner :{ban,2000000276,728,90,ban} <br> <br>banner 2 :{ban,3000000344,728,90,ban}<br>" +
"banner 3:{ban,2000000345,468,60,ban}<br>banner 4:{ban,3000000369,120,600,ban}<br>";
String matchRegex= ".*" + regexBan + ".*" ;
Pattern p = Pattern.compile(matchRegex,Pattern.DOTALL );
int cnt = 0;
while(true){
Matcher match = p.matcher(emailContent);
System.out.println("matches:"+match.matches());
if(match.matches()){
cnt++;
String Id=match.replaceFirst("$1");
String width=match.replaceFirst("$2");
String height=match.replaceFirst("$3");
System.out.println("para:"+Id+":"+width+":"+height);
emailContent=emailContent.replaceFirst(regexBan,Id + width + height);
System.out.println("email content:" + emailContent );
}else break;
}
}结果:
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :{ban,3000000344,728,90,ban}<br>banner 3:{ban,2000000345,468,60,ban}<br>banner 4:{ban,3000000369,120,600,ban}<br>
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :3000000369120600<br>banner 3:{ban,2000000345,468,60,ban}<br>banner 4:{ban,3000000369,120,600,ban}<br>
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :3000000369120600<br>banner 3:3000000369120600<br>banner 4:{ban,3000000369,120,600,ban}<br>
matches:true
para:3000000369:120:600
email content:test banner <br> <br>banner :3000000369120600 <br> <br>banner 2 :3000000369120600<br>banner 3:3000000369120600<br>banner 4:3000000369120600<br>
matches:false
改为String matchRegex= ".*?" + regexBan + ".*" ;代码好怪异,为什么要这么做呢?
直接这样不就可以了吗
emailContent=emailContent.replaceAll(regexBan, "$1$2$3");
System.out.println(emailContent);