正则实现:字符串:中华人民,大华人们,我们华人一起,华人替换关键字“华人” 条件:除去'中华人民','大华人们'等词组外,所有'华人'的词组
均替换成AA例如上面字符串 替换后是:
中华人民,大华人们,我们AA一起,AA
均替换成AA例如上面字符串 替换后是:
中华人民,大华人们,我们AA一起,AA
解决方案 »
- 谁会ssl呀 和ssh结合使用
- 上传下载时怎么做到先写缓存,有空再写硬盘。。java,apache,axis2
- 郁闷我的Thread还是锁不住,请大家帮忙看看
- myeclipse的调试功能
- 请教各位,目前使用websphere哪个版本作为开发环境。
- 任务发送这个如何设计开发呢
- 有关Eclipes的问题?
- java代码执行顺序问题
- 新手提问
- 大型系统设计(例如SAP)讨论
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- spring-security2.0.5 @Secured({"ROLE_ADMIN"}) 不起作用
String str = "中华人民,大华人们,我们华人一起,华人";
Matcher matcher = Pattern.compile("华人").matcher(str);
StringBuffer sbr = new StringBuffer();
while (matcher.find()) {
int i = matcher.start();
if(i+3 < str.length()-1 && (str.substring(i-1,i+3).equals("中华人民") || str.substring(i-1,i+3).equals("大华人们")))
continue;
matcher.appendReplacement(sbr, "Java");
}
matcher.appendTail(sbr);
System.out.println(sbr.toString());
String str = "中华人民,大华人们,我们华人一起,华人"
str = str .sunbstring(9,str.length()).replaceAll("华人","AA");
if(i+3 < str.length()-1 && i-1 > 0 &&(str.substring(i-1,i+3).equals("中华人民") || str.substring(i-1,i+3).equals("大华人们")))
public static void main(String[] args) {
String s = "中华人民,大华人们,我们华人一起,华人";
String r = "(,?)(.*?华人.*?)(,?)";
Pattern p = Pattern.compile(r);
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String news = m.group(1) + "AA" + m.group(3);
m.appendReplacement(sb, news);
}
m.appendTail(sb);
System.out.println(sb);
}
String str ="中华人民,大华人们,我们华人一起,华人";
String regex1= "((?<!\u4e2d)\u534e\u4eba(?!\u6c11)|(?<!\u4e2d)\u534e\u4eba(?=\u6c11)|(?<=\u4e2d)\u534e\u4eba(?!\u6c11))";
String regex2= "((?<!\u5927)\u534e\u4eba(?!\u4eec)|(?<!\u5927)\u534e\u4eba(?=\u4eec)|(?<=\u5927)\u534e\u4eba(?!\u4eec))";
Matcher m1 = Pattern.compile(regex1).matcher(str);
Matcher m2 = Pattern.compile(regex2).matcher(str);
while(m2.find()&&m1.find()){
int i1 = m1.start();
int i2 = m2.start();
if(i1==i2){
str = str.substring(0, i1)+"AA"+str.substring(m1.end(), str.length());
}
System.out.println(str);
}
弱弱的问一下 ,这里的 u6c11 u4eba 都是啥
改成这样String str ="中华人民,大华人们,我们华人一起,华人";
String regex1= "((?<!\u4e2d)\u534e\u4eba(?!\u6c11)|(?<!\u4e2d)\u534e\u4eba(?=\u6c11)|(?<=\u4e2d)\u534e\u4eba(?!\u6c11))";
String regex2= "((?<!\u5927)\u534e\u4eba(?!\u4eec)|(?<!\u5927)\u534e\u4eba(?=\u4eec)|(?<=\u5927)\u534e\u4eba(?!\u4eec))";
Matcher m1 = Pattern.compile(regex1).matcher(str);
Matcher m2 = Pattern.compile(regex2).matcher(str);
while(m2.find()){
int i2 = m2.start();
while(m1.find()){
int i1 = m1.start();
if(i1==i2){
str = str.substring(0, i1)+"AA"+str.substring(m1.end(), str.length());
}
}
m1.reset();
System.out.println(str);
}
先找到所有“华人”字段的index
然后找所有中华人民,大华人们的index
然后中华人民和大华人们的index+1
然后和华人的index匹配,
之后根据华人中所有没有匹配到的index,把对应的“华人”删除
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class TextRegexZhongHua {
public static void main(String args[]) {
String str="水木年华人都喜欢,中华人民,大华人们,我们华人一起,华人";
Matcher m=Pattern.compile("(?<![\u4e2d\u5927])(?<!(\u6c34\u6728\u5e74))(\u534e\u4eba)(?![\u6c11\u4eec])").matcher(str);
//找到前面没有水木年、中、大并且后面没有民、们的华人两字
while(m.find()) {
str=str.substring(0,m.start())+"AA"+str.substring(m.end());
}
System.out.println(str);
}
}测试结果:水木年华人都喜欢,中华人民,大华人们,我们AA一起,AA按照上面这个例子,你可以按自己的需求来替换关键字,注意使用JDK自带的native2ascii.exe工具,还有就是不要使用replace(String regex,"AA");这种方法,它和(?<!)这种符号使用时很不理想..
String str = "中华人民,大华人们中华人民,我们华人一起,华人";
Matcher matcher = Pattern.compile("华人").matcher(str);
StringBuffer sbr = new StringBuffer();
while (matcher.find()) {
int i = matcher.start();
if(i+3 < str.length()-1 && i-1 > -1 &&(str.substring(i-1,i+3).equals("中华人民") || str.substring(i-1,i+3).equals("大华人们")))
continue;
matcher.appendReplacement(sbr, "AA");
}
matcher.appendTail(sbr);
System.out.println(sbr.toString());
把我的重发一遍,如果还有其他的词不需要替换,可以考虑把if判断的或者里面的判断拿出来写成一个方法。
import java.util.regex.Pattern;public class TextRegexZhongHua {
public static void main(String args[]) {
String str="水木年华人都喜欢,中华人民,大华人们,我们华人一起,华人,中华人们,大华人民";
Matcher m1=Pattern.compile("(((?<![中])(?<!水木年)华人(?![民]))|((?<=[中])(?<!水木年)华人(?![民]))|((?<![中])(?<!水木年)华人(?=[民])))").matcher(str);
//找到前面没有水木年、中、大并且后面没有民、们的华人两字
Matcher m2=Pattern.compile("(((?<![大])(?<!水木年)华人(?![们]))|((?<=[大])(?<!水木年)华人(?![们]))|((?<![大])(?<!水木年)华人(?=[们])))").matcher(str);
while(m1.find()) {
while(m2.find()) {
if(m1.start()==m2.start()) {
str=str.substring(0,m1.start())+"AA"+str.substring(m1.end());
}
}
m2.reset();
}
System.out.println(str);
}
}
测试结果:水木年华人都喜欢,中华人民,大华人们,我们AA一起,AA,中AA们,大AA民