Java-Negative Lookbehind

Negative Lookbehind

Negative lookbehind is usually useful if we want to match something not proceeded by something else. We can make our own negative lookbehinds with the lookbehind operator (?<!). For example, to match two characters not proceeded by four digit, we could use :

 

More Examples

5 34

 

Negative lookbehind After the Match Examples

43

 

Program

Program Source

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Javaapp {
  
    public static void main(String[] args) {
        
        Pattern pat = Pattern.compile("\\w+\\b(?<!it)");
                        
        Matcher mat = pat.matcher("achmit abens aconit admet acquit flees adamit");
         
        while(mat.find())
        {
            System.out.println(mat.group());
        }
    }
}

Leave a Comment