62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test {
public static void main(String[] args) {
String s = "find 关键词A 关键词B 字段名1=关键词1 not 字段名2=关键词2 关键词3 or " +
"字段名3=关键词4 关键词5 关键词6 关键词7 xor 关键词8 关键词9 " +
"TS=TO 2008-06-11, FR 2007-06-11 " +
"and 字段名4=关键词10 关键词11 or 关键词12";
Pattern p = Pattern.compile("(?<=(?:^find|=|x?or|not|and) ?)(?<!TS=)((?!x?or|not|and)[^ =]+(?: |$))+");
Matcher m = p.matcher(s);
while(m.find()) {
System.out.println(m.group());
}
}
}
关键词A 关键词B
关键词1
关键词2 关键词3
关键词4 关键词5 关键词6 关键词7
关键词8 关键词9
关键词10 关键词11
关键词12
再问一个若若的问题 [^ =]++ 这个是什么意思呀? [^ =]+表示 非空格和=号有1到N个 后面再加个+号就不清楚了?哪们高手能给解析一下吗?
String s = "find 字段名1=关键词1 not 字段名2=关键词2 or 字段名3=关键词3 "
+ "xor 关键词4 关键词6 TS=TO 2008-06-11, FR 2007-06-11 and 字段名4=关键词5 关键词7";
再问一个若若的问题 [^ =]++ 这个是什么意思呀? [^ =]+表示 非空格和=号有1到N个 后面再加个+号就不清楚了?哪们高手能给解析一下吗?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test3 {
public static void main(String[] args) {
String s = "find 字段名1=关键词1 not 字段名2=关键词2 关键词3 or " +
"字段名3=关键词4 关键词5 关键词6 关键词7 xor 关键词8 关键词9 " +
"TS=TO 2008-06-11, FR 2007-06-11 " +
"and 字段名4=关键词10 关键词11 or 关键词12";
Pattern p = Pattern.compile("(?<=(?:=|x?or|not|and) ?)(?<!TS=)((?!x?or|not|and)[^ =]+(?: |$))+");
Matcher m = p.matcher(s);
while(m.find()) {
System.out.println(m.group());
}
}
}
关键词1
关键词2 关键词3
关键词4 关键词5 关键词6 关键词7
关键词8 关键词9
关键词10 关键词11
关键词12
while(m.find()) {
String[] str = m.group().split(" ");
for(int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test3 {
public static void main(String[] args) {
String s = "find 字段名1=关键词1 not 字段名2=关键词2 or 字段名3=关键词3 " +
"xor 关键词4 and 字段名4=关键词5 TS=TO 2008-06-11, FR 2007-06-11 ";
Pattern p = Pattern.compile("(?<=(?:=|x?or|not|and) ?)(?<!TS=)[^ =]++(?! *=)");
Matcher m = p.matcher(s);
while(m.find()) {
System.out.println(m.group());
}
}
}