package com.mindprod.example;
import static java.lang.System.out;
import java.util.regex.Pattern;
/**
* demonstrate how to use a negative regex
* <p/>
* composed with IntelliJ IDEA
*
* @author Roedy Green, Canadian Mind Products, based on example posted by Stefan Ram
* @version 1.0, 2006-03-02
*/
public final class TestNegativeRegex
{
/**
* regex pattern that rejects strings of form "my connector is broken" but accepts "my anythingelse is broken". It
* uses lookahead. You can't have a purely negative regex. You must have some sort of positive match.
*/
private static final Pattern pattern =
Pattern.compile( "(?!my connector)my [a-z]* is broken" );
/**
* check negative regex
*
* @param args not used
*/
public static void main( String[] args )
{
out.println( pattern.matcher( "my connector is broken" ).matches() );// false
out.println( pattern.matcher( "my engine is broken" ).matches() );// true
out.println( pattern.matcher( "my engine is working" ).matches() );// false
}
}