import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NegativeLookaheadExample {
public static void main(String args[]) throws Exception {
String regex = "John (?!Smith)[A-Z]\\w+";
Pattern pattern = Pattern.compile(regex);
String candidate = "I think that John Smith ";
candidate += "is a fictional character. His real name ";
candidate += "might be John Jackson, John Westling, ";
candidate += "or John Holmes for all we know.";
Matcher matcher = pattern.matcher(candidate);
String tmp = null;
while (matcher.find()) {
tmp = matcher.group();
System.out.println("MATCH:" + tmp);
}
}
}
|