求一正则表达式

wcjok 2007-09-26 02:56:21
public class Test01 {
public static void main(String[] args) {
String regex=""
String s="/abc";
Matcher matcher = Pattern.compile(regex).matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}
}

求正则表达式regex,能够实现s不是"/abc",就可以把s输出出来。
...全文
158 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyifei211 2007-09-28
  • 打赏
  • 举报
回复
package ch01;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test3 {
public static void main(String[] args) {
String str[] =new String[]{"abcd","123","adc","1abc","eabcd"};

Pattern pt=Pattern.compile(".*abc.*");
for(int i=0;i<str.length;i++){
Matcher m=pt.matcher(str[i]);
boolean b=m.matches();
if(!b)
System.out.println(str[i]);
}
}
}
最简单也是最有效的方法,只输出不含有abc的
shan1119 2007-09-28
  • 打赏
  • 举报
回复
哦,这回好象可以了:
String str[] =new String[]{"abcd","123","adc","1abc","eabcd"};
Pattern p = Pattern.compile("^(?!.*abc)(.*)$");
for(int i=0;i<str.length;i++){
Matcher m = p.matcher(str[i]);

if(m.find()){
System.out.println(m.group(1));
}
}
shan1119 2007-09-27
  • 打赏
  • 举报
回复
String str[] =new String[]{"abcd","123","adc","1abc"};
Pattern p = Pattern.compile("(?!abc)^(.*)$(?<!abc)");
for(int i=0;i<str.length;i++){
Matcher m = p.matcher(str[i]);

if(m.find()){
System.out.println(m.group(1));
}
}
好像可以.
wcjok 2007-09-27
  • 打赏
  • 举报
回复
public class Test3 {
public static void main(String[] args) {
String[] as = {"/abcw", "isudygq",
"abchiod", "shdfabc", "dasbec"};
for(String s : as)
if(!s.matches(".*abc.*"))
System.out.println(s);
}
}
我不想if(!s.matches(".*abc.*"))用“非”操作,我的意思我的正则表达式,直接就能判断出来
zephyr_cc 2007-09-27
  • 打赏
  • 举报
回复
这意思?

public class Test3 {
public static void main(String[] args) {
String[] as = {"/abcw", "isudygq",
"abchiod", "shdfabc", "dasbec"};
for(String s : as)
if(!s.matches(".*abc.*"))
System.out.println(s);
}
}
zephyr_cc 2007-09-27
  • 打赏
  • 举报
回复
"eabcd"这样好像就不行了.....
wcjok 2007-09-26
  • 打赏
  • 举报
回复
我知道,很多办法都能实现,但是我因为需求和其它系统集成,必须要用正则表达式
shan1119 2007-09-26
  • 打赏
  • 举报
回复
写不出来..........可以用indexOf
aqy_19781228 2007-09-26
  • 打赏
  • 举报
回复
好像已经解决了
for_cyan 2007-09-26
  • 打赏
  • 举报
回复
你replace就可以了
replace("");
wcjok 2007-09-26
  • 打赏
  • 举报
回复
可能是我的意思没有说明白

public static void main(String[] args) {
String regex="([^/abc])";
String s="/abcw";
Matcher matcher = Pattern.compile(regex).matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}

如果是上面的代码话,会输出w,我的意思是只要包含abc,就什么也不输出
shan1119 2007-09-26
  • 打赏
  • 举报
回复
漏逗好了,不好意思.拷贝楼主的代码...
shan1119 2007-09-26
  • 打赏
  • 举报
回复
非要用的话:

public class Test01 {
public static void main(String[] args) {
String regex="(/abc)"
String s="/abc";
Matcher matcher = Pattern.compile(regex).matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}
}

shan1119 2007-09-26
  • 打赏
  • 举报
回复
if(!s.equals("/abc"))System.out.println(s);
for_cyan 2007-09-26
  • 打赏
  • 举报
回复
[^/abc]

62,623

社区成员

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

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