I have some code:
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());
This was supposed to print all lines except third and fourth, because they contain "if" and "while" words. But in reality it prints nothing. Every help would be grateful. Thank you.
Update:
Thank you guys for your answers! Your examples are working. I have one additional question: after negative lookahead I want to insert condition .*\\(.*\\).*\\{
which means that text includes .*<negotiation>.*(.*).*{
In a simple manner, it should print the second line from my String test
. I tried this one regex (?m)^.*(?!(catch|if|while|try|return|finally|new|throw).\\(.*\\).*\\{)*$
but it doesn't work in a proper way. What would you suggest?