go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Lookahead and lookbehind assertions
 
Subject: Lookahead and lookbehind assertions
Author: Alex_Raj
In response to: Special characters in regular expression
Posted on: 08/13/2015 01:49:17 AM


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


 

> On 08/13/2015 12:35:51 AM Alex_Raj wrote:

The following characters are metacharacters used in constructing regular expression. They must be escaped by '\' if they are used as their own literal characters.

Metacharacters
\      -- The backslash
[]     -- Matches a single character that is contained within the brackets.
[^ ]   -- Matches a single character that is NOT contained within the brackets.
[ && ] -- Denotes a intersection like [a-g&&[e-z]], which matches 'e', 'f', or 'g'
[ - ]  -- Denotes a range like [a-z]. Otherwise, like [abc-], it a literal character.
^      -- Matches the starting position within the string.
$      -- Matches the ending position within the string.
()     -- Defines a group or scope.
|      -- Logical OR
*      -- Matches the preceding element zero or more times. For example, 'ab*' matches 'a', 'ab', 
       -- 'abb', etc. '(ab)*' matches '', 'ab', 'abab', and so on. '[ab]*' matches '', 'a', 'b',
       -- 'ab', 'ba', 'aabab', and so on.
?      -- Matches the preceding element zero or one time. x? = (x|E)
+      -- Matches the preceding element one and more times. x+ = xx*
{m}    -- Matches the preceding element exactly m times.
{m,}   -- Matches the preceding element at least m times.
{m,n}  -- Matches the preceding element at least m but not more than n times.



Predefined characters

.  -- Any single character, for example, '.at' matches 'bat' 'cat', or 'hat'. But it is 
   -- literal character within [ ], for example, '[.]at' matches '.at' only.
\d -- A digit: [0-9]
\D -- A non-digit: [^0-9]
\s -- A whitespace: [ \t\n\x0B\f\r]
\S -- A non-whitespace: [^\s]
\w -- A word character: [a-zA-Z0-9_]
\W -- A non-word character: [^\w]






References:

 


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