去掉字符串中匹配 的字符串
复制代码 代码如下:
/** * 去掉字符串中匹配 的字符串 * * @author zhujie * @return String regex 要替换的内容 value 字符串 state 替换的内容变成什么 */ public static String toRegex(String regex, String value, String state) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(value); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, state); } m.appendTail(sb); return sb.toString(); }
复制代码 代码如下:
public static void main(String[] args) { * String regex = "\\p{Lower}"; //去掉里面的小写字符 regex ="'"; //去掉里面的 ' * regex="\\p{Sc}"; //去掉货币符号 $ regex="\\p{Punct}"; //去掉里面的所有的符号 * regex="\""; //去掉 " regex ="<.>"; //去掉里面的html 标签 * * String value="fjdks<sss>jfl'fdk$$jfl"; * System.out.println(toRegex(regex,value,"")); */ }
查看更多关于java 字符串匹配函数_正则表达式的详细内容...