【发布时间】:2014-01-15 10:01:12
【问题描述】:
我有一些代码:
String test = "int measure; \n" +
"void introduce() { \n" +
"while (line != null) { \n" +
"if(measure > 0) { \n" +
"System.out.println(smile); \n" +
"} \n" +
"}";
String functions = "^.*(?!(catch|if|while|try|return|finally|new|throw)).*$";
Pattern patternFunctions = Pattern.compile(functions);
Matcher matcherFunctions = patternFunctions.matcher(test);
while(matcherFunctions.find())
System.out.println(matcherFunctions.group());
这应该打印除第三和第四行之外的所有行,因为它们包含“if”和“while”字样。但实际上它什么也没打印。 每一个帮助将不胜感激。 谢谢。
更新:
谢谢大家的回答!你的例子很有效。我还有一个问题:在负前瞻之后,我想插入条件.*\\(.*\\).*\\{,这意味着文本包含.*<negotiation>.*(.*).*{,以一种简单的方式,它应该从我的String test 中打印第二行。我尝试了这个正则表达式(?m)^.*(?!(catch|if|while|try|return|finally|new|throw).\\(.*\\).*\\{)*$,但它不能以正确的方式工作。你有什么建议?
【问题讨论】: