正则表达式如何替换匹配的一部分字符串 Pattern pattern = Pattern.compile("(&|\\)(this is my target)=[^&]*");我只想替换 this is my target 这一段,应该怎么办啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.exampledepot.com/egs/java.util.regex/GroupInRep.html// Compile regular expression String patternStr = "\\((\\w+)\\)"; String replaceStr = "<$1>"; Pattern pattern = Pattern.compile(patternStr); // Replace all (\w+) with <$1> CharSequence inputStr = "a (b c) d (ef) g"; Matcher matcher = pattern.matcher(inputStr); String output = matcher.replaceAll(replaceStr); // a (b c) d <ef> g 只想替换 this is my target??那就用replaceAll函数啊。 Collection和Collections,有点乱啊 如何给泛型加上<>让eclipse停止警告 使用compass lucene 中文搜索不出来。。。 javax.mail.Transport类 java中回调机制和别名机制有什么区别 安装多JDK问题 关于抽象方法的. JFrame窗体怎么一开始就让它最大化? 求:哪里有开发一个完整java项目实例的书,谢谢 請問誰能描述一下這道SCJP題的運行過程? 怎样在xml中定义校验字符串的正则表达式 更新Vector<?>报错
String patternStr = "\\((\\w+)\\)";
String replaceStr = "<$1>";
Pattern pattern = Pattern.compile(patternStr);
// Replace all (\w+) with <$1>
CharSequence inputStr = "a (b c) d (ef) g";
Matcher matcher = pattern.matcher(inputStr);
String output = matcher.replaceAll(replaceStr);
// a (b c) d <ef> g
那就用replaceAll函数啊。