Regex Boundary Matchers in Java

Prerequisite- Regular Expressions in Java

Boundary matches can help us find where in the string match is taking place. You can make your pattern matches more precise by specifying such information with boundary matchers. For example, maybe you’re interested in finding a particular word, but only if it appears at the beginning or end of a line. Or maybe you want to know if the match is taking place on a word boundary, or at the end of the previous match.

List of boundary Matchers

  • ^ – Placed before the word to match
  • $ – Placed at the end of a word to match
  • \b – Checks whether a pattern begin or end on a word boundary
  • \B – Matches the expression on a non-word boundary
  • \AThe beginning of the input
  • \GRequires to match to occur only at the end of the previous match
  • \ZThe end of the input but for the final terminator, if any
  • \zThe end of the input

Case 1: Matching the  word with ^ and $

  • ^ – matches the beginning of a line
  • $ – matches the end.
  • Input : txt = "geeksforgeeks", regex = "^geeks"
    Output : Found from index 0 to 3
    Explanation : Note that the result doesn't include "geeks" after
                  "for" as we have used ^ in regex.
  • Input : txt = "geeksforgeeks", regex = "geeks$"
    Output : Found from index 8 to 13.
    Explanation : Note that the result doesn't include "geeks" before 
                 "for" as we have used $ in regex.
  • Input : txt = "geeksforgeeks", regex = "^geeks$"
    Output : No match found
    Explanation : The given regex would only matches with "geeks".
  • Input : txt = "  geeksforgeeks", regex = "^geeks"
    Output: No match found.
    Explanation : The input string contains extra whitespace at the beginning.
  • // Extra \ is used to escape one \
    Input : txt = "  geeksforgeeks", regex : "^\\s+geeks"
    Output: Found from index 0 to 6.
    Explanation : The pattern specifies geeks after one or more spaces.
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate that ^ matches the beginning of
// a line, and $ matches the end.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
class Reg
{
    public static void main(String[] args)
    {
        String txt = "geeksforgeeks";
  
        // Demonstrating ^
        String regex1 = "^geeks";
        Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = pattern1.matcher(txt);
        while (matcher1.find())
        {
            System.out.println("Start index: " + matcher1.start());
            System.out.println("End index: " + matcher1.end());
        }
  
        // Demonstrating $
        String regex2 = "geeks$";
        Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
        Matcher matcher2 = pattern2.matcher(txt);
        while (matcher2.find())
        {
            System.out.println("\nStart index: " + matcher2.start());
            System.out.println("End index: " + matcher2.end());
        }
    }
}

chevron_right


Output:

Start index: 0
End index: 5

Start index: 8
End index: 13

 

Case 2 : Checks whether a pattern begin or end on a word boundary using \b

  • Input: txt = "geeksforgeeks geekspractice", pat = "\\bgeeks"
    Output: Found from index 0 to 5 and from index 14 to 19
    Explanation : The pattern "geeks" is present at the beginning
                  of two words "geeksforgeeks" and "geekspractice"
    
  • Input: txt = "geeksforgeeks geekspractice", pat = "geeks\\b"
    Output: Found from index 8 to 13
    Explanation : The pattern "geeks" is present at the end of one
                  word "geeksforgeeks"
    
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to demonstrate use of \b to match
// regex at beginning and end of word boundary
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
class Reg
{
    public static void main(String[] args)
    {
        String txt = "geeksforgeeks geekspractice";
  
        // Demonstrating beginning of word boundary
        String regex1 = "\\bgeeks"; // Matched at two places
        Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = pattern1.matcher(txt);
        while (matcher1.find())
        {
            System.out.println("Start index: " + matcher1.start());
            System.out.println("End index: " + matcher1.end());
        }
  
        // Demonstrating end of word boundary
        String regex2 = "geeks\\b"; // Matched at one place
        Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
        Matcher matcher2 = pattern2.matcher(txt);
        while (matcher2.find())
        {
            System.out.println("\nStart index: " + matcher2.start());
            System.out.println("End index: " + matcher2.end());
        }
    }
}
chevron_right

Output:

Start index: 0
End index: 5
Start index: 14
End index: 19

Start index: 8
End index: 13

 

Case 3 : Match the expression on a non-word boundary, use \B instead

  • Input: txt = "geeksforgeeks geekspractice", pat = "\\Bgeeks"
    Output: Found from index 8 to 13
    Explanation : One occurrence  of pattern "geeks" is not present at
                  the beginning of word which is end of "geeksforgeeks"
    
  • Input: txt = "geeksforgeeks geekspractice", pat = "geeks\\B"
    Output: Found from index 0 to 5 and from index 14 to 19
    Explanation : Two occurrences of "geeks" are not present at the end
                  of word.
    
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to demonstrate use of \B to match
// regex at beginning and end of non word boundary
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
class Reg
{
    public static void main(String[] args)
    {
        String txt = "geeksforgeeks geekspractice";
  
        // Demonstrating Not beginning of word
        String regex1 = "\\Bgeeks"; // Matches with two
        Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = pattern1.matcher(txt);
        while (matcher1.find())
        {
            System.out.println("Start index: " + matcher1.start());
            System.out.println("End index: " + matcher1.end() + "\n");
        }
  
        // Demonstrating Not end of word
        String regex2 = "geeks\\B"; // Matches with one
        Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
        Matcher matcher2 = pattern2.matcher(txt);
        while (matcher2.find())
        {
            System.out.println("Start index: " + matcher2.start());
            System.out.println("End index: " + matcher2.end());
        }
    }
}
chevron_right

Output:

Start index: 8
End index: 13

Start index: 0
End index: 5
Start index: 14
End index: 19

 

Case 4: Match to occur only at the end of the previous match, use \G:

  • Input: txt = "geeksgeeks geeks", pat = "\\Ggeeks"
    Output: Found from index 0 to 5 and from 5 to 10
    Explanation : Only first two occurrences of "geeks" in text
                  match. the occurrence after space doesn't match
                  as it is not just after previous match.
    
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to demonstrate use of \G to match
// to occur only at the end of the previous match
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
class Reg
{
    public static void main(String[] args)
    {
        String txt = "geeksgeeks geeks";
  
        // Demonstrating \G
        String regex1 = "\\Ggeeks"; // Matches with first two geeks
        Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = pattern1.matcher(txt);
        while (matcher1.find())
        {
            System.out.println("Start index: " + matcher1.start());
            System.out.println("End index: " + matcher1.end());
        }
    }
}
chevron_right

Output:

Start index: 0
End index: 5
Start index: 5
End index: 10

References:https://docs.oracle.com/javase/tutorial/essential/regex/bounds.html

This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.