go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Lookahead assertion examples
 
Subject: Lookahead assertion examples
Author: Alex_Raj
In response to: Lookahead and lookbehind assertions
Posted on: 08/13/2015 07:16:49 PM

Example: Find empty string '' which is immediately followed by lower-case character

    String regex = "(?=[a-z])"; 
    String input = "123a567b9";
    System.out.println("Regex: " + regex);
    System.out.println("Input: " + input);
	
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
		
    boolean found = false;
    while (matcher.find()) {
        System.out.println("Found '" + matcher.group() + 
                           "' from " + matcher.start() + 
                           " to " + matcher.end());
        found = true;
    }

    if(!found)
        System.out.println("No match found!");



The ouput:

    Regex: (?=[a-z])
    Input: 123a567b9
    Found '' from 3 to 3
    Found '' from 7 to 7




Example: Find string which 1) starts with '' and 2) is immediately followed by lower-case character and 3) has total length of 2 any characters
    String regex = "(?=[a-z]).{2}"; 
    String input = "123a567b9";



The ouput:

    Regex: (?=[a-z]).{2}
    Input: 123a567b9
    Found 'a5' from 3 to 5
    Found 'b9' from 7 to 9




Example: Find string which 1) starts with '' and 2) is immediately followed by lower-case character and 3) has total length of 3 any characters
    String regex = "(?=[a-z]).{3}"; 
    String input = "123a567b9";



The ouput:

    Regex: (?=[a-z]).{3}
    Input: 123a567b9
    Found 'a56' from 3 to 6



 

> On 08/13/2015 01:49:17 AM Alex_Raj wrote:


a(?=b)	-- positive lookahead:  Is there any item matches 'a' which is followed by 'b'?
a(?!b)	-- negative lookahead:  Is there any item matches 'a' which is NOT followed by 'b'?
(?<=a)b	-- positive lookbehind: Is there any item matches 'b' which is led by 'a'?
(?<!a)b	-- negative lookbehind: Is there any item matches 'b' which is NOT led by 'a'?


The simpler forms:
(?=x)	-- positive lookahead:  Is there any item matches '' which is followed by 'x'?
(?!x)	-- negative lookahead:  Is there any item matches '' which is NOT followed by 'x'?
(?<=x)	-- positive lookbehind: Is there any item matches '' which is led by 'x'?
(?<!x)	-- negative lookbehind: Is there any item matches '' which is NOT led by 'x'?





Example: Lookahead assertion -- Find the 'black' color used for 'cat'.

    String regex = "black(?= cat)"; 
    String input = "The black cat wears a black hat.";
    System.out.println("Regex: " + regex);
    System.out.println("Input: " + input);
	
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
		
    boolean found = false;
    while (matcher.find()) {
        System.out.println("Found '" + matcher.group() + 
                           "' from " + matcher.start() + 
                           " to " + matcher.end());
        found = true;
    }

    if(!found)
        System.out.println("No match found!");



The ouput:

    Regex: black(?= cat)
    Input: The black cat wears a black hat.
    Found 'black' from 4 to 9




Example: Lookahead assertion -- Find the 'black' color used NOT for 'cat'.

    String regex = "black(?! cat)"; 
    String input = "The black cat wears a black hat.";
    System.out.println("Regex: " + regex);
    System.out.println("Input: " + input);
	
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
		
    boolean found = false;
    while (matcher.find()) {
        System.out.println("Found '" + matcher.group() + 
                           "' from " + matcher.start() + 
                           " to " + matcher.end());
        found = true;
    }

    if(!found)
        System.out.println("No match found!");



The ouput:

    Regex: black(?! cat)
    Input: The black cat wears a black hat.
    Found 'black' from 22 to 27




Example: Lookbehind assertion -- Find the 'cat' with 'black' color.

    String regex = "(?<=black )cat"; 
    String input = "The black cat and the white cat.";
    System.out.println("Regex: " + regex);
    System.out.println("Input: " + input);
	
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
		
    boolean found = false;
    while (matcher.find()) {
        System.out.println("Found '" + matcher.group() + 
                           "' from " + matcher.start() + 
                           " to " + matcher.end());
        found = true;
    }

    if(!found)
        System.out.println("No match found!");



The ouput:

    Regex: (?<=black )cat
    Input: The black cat and the white cat.
    Found 'cat' from 10 to 13




Example: Lookbehind assertion -- Find the 'cat' with NOT 'black' color.

    String regex = "(?<!black )cat"; 
    String input = "The black cat and the white cat.";
    System.out.println("Regex: " + regex);
    System.out.println("Input: " + input);
	
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
		
    boolean found = false;
    while (matcher.find()) {
        System.out.println("Found '" + matcher.group() + 
                           "' from " + matcher.start() + 
                           " to " + matcher.end());
        found = true;
    }

    if(!found)
        System.out.println("No match found!");



The ouput:

    Regex: (?<!black )cat
    Input: The black cat and the white cat.
    Found 'cat' from 28 to 31





References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.