Java-Lookbehind

Lookbehind

Lookbehind is just like Lookahead. They only assert whether immediate portion behind a given input string’s current portion is suitable for a match or not. We can make our own lookbehinds with the lookbehind operator (?<=).

Lookbehind Before the Match Examples

3

 

Lookbehind with Lookahead

4

 

Lookbehind After the Match Examples

3 4

 

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("\\d+(?<=INR\\d{3})");
        
        Matcher mat = pat.matcher("USD100 INR200 EUR300 INR400 AED500");
        
        while(mat.find())
        {
            System.out.println("INR = "+mat.group());
        }
    }
}

Leave a Comment