Source Code : Negative Look ahead

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Negative Look ahead

Negative Look ahead

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);
    }
  }
}

           
       

Thank with us