引包
import java.util.regex.Matcher;
import java.util.regex.Pattern;
方法1:
// 匹配 #开头结尾中,#以及中间得字符串 #xxx 替换为 "" #123 匹配#123
String tableModle = "#123#2#3#4";
Pattern p=Pattern.compile("#(.+?)(?=)");
Matcher m=p.matcher(tableModle);
tableModle = m.replaceAll("");
最后tableModle为:
方法2:
// #xxx 替换为 #123 匹配#123
String tableModle = "#123#2#3#4";
Pattern p=Pattern.compile("#.*?"); //此正则只匹配一个
Matcher m=p.matcher(tableModle);
boolean flag = m.find();
while(flag){
tableModle = tableModle.replace(m.group(), "");
m=p.matcher(tableModle);
flag = m.find();
}
System.out.println(tableModle); //
方法3:
// 匹配 #开头结尾中, 中间的字符串 #123 匹配123
String tableModle = "#123#2#3#4";
Pattern p=Pattern.compile("(?<=#).*?(?=)");
Matcher m=p.matcher(tableModle);
boolean flag = m.find();
while(flag){
tableModle = tableModle.replace(“#”+m.group(), "");
m=p.matcher(tableModle);
flag = m.find();
}
最后tableModle为: