正则怎么排除特定词

pcmlose 2014-03-23 04:00:16
比如:
String str="我你我你我我我我们你们们你你我我我们你你你你们我";
输出:我你我你我我我
若是
String str="我你我你我我我我1们你们们你你我我我1们你你你你们我";
输出:我你我你我我我我1们你们们你你我我我1们你你你你们我

[^我们]+
只能是单个字的,怎么是一个词?
...全文
591 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-03-24
  • 打赏
  • 举报
回复
是替换掉的意思/?
Inhibitory 2014-03-24
  • 打赏
  • 举报
回复
public class Test {
	public static void main(String[] args) {
		String str = "我你我你我我我我们你们们你你我我我们你你你你们我";
		System.out.println(str.replaceAll("我们|你们", ""));
	}
}
Inhibitory 2014-03-24
  • 打赏
  • 举报
回复
[^词组]+ 比如,匹配除"我们"这个词外的所有字符 正则表达式里没有这种用法。
pcmlose 2014-03-24
  • 打赏
  • 举报
回复
我的解决方案: ((?!我们).)+ 勉强满足要求 结帖,谢谢各位
pcmlose 2014-03-23
  • 打赏
  • 举报
回复
我不知道用什么词来表达就用排除了- - 简单的说就是实现 [^词组]+ 比如,匹配除"我们"这个词外的所有字符 str="你我我们我你" 输出: --- 你我 我你 --- 想一天了
QQ2472322319 2014-03-23
  • 打赏
  • 举报
回复

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author y
*/
public class Main {
public static void main(String[] argc){
String str="我你我你我我我我们你们们你你我我我们你你你你们我";
Pattern p = Pattern.compile("[^我们]+");
String[] t = p.split(str);
for(int i=0;i<t.length;i++)
{
System.out.println(t[i]);
}
}
}

这段可以参考下 [^我们]+ 表示多个除‘我’或’们‘的字符组成的串
上述代码通过该正则分割字符串
tony4geek 2014-03-23
  • 打赏
  • 举报
回复
没看懂,,,
momoyssy 2014-03-23
  • 打赏
  • 举报
回复
正则表达式并不是用来排除的,是用来匹配的。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧