regex

Greedy and non-greedy Reg Ex matching

This is an example of how to use greedy and non-greedy regular expression matching. Greedy matching means that the expression will match as large a group as possible, while non-greedy means it will match the smallest group possible. Matching with greedy and non-greedy regular expressions implies that you should:

  • Compile a String regular expression to a Pattern, using compile(String regex) API method of Pattern.
  • Use matcher(CharSequence input) API method of Pattern to create a Matcher that will match the given String input against this pattern.
  • Use find() API method to find the next subsequence of the input sequence that matches the pattern.
  • Follow the above steps for greedy and non-greedy regular expressions. A greedy regular expression is a word character followed by any character one or more times, while a non-greedy regular expression is a word character followed by any character one or more times and followed by the ?, that means that the first group found that matches the pattern is returned.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

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

public class GreedyAndNonGreedyRegExMatching {
	
	public static void main(String[] args) {
		
		Pattern pattern;
		Matcher matcher;
		
		// Greedy quantifiers
		
		pattern = Pattern.compile("A.*c"); // Greedy
		matcher = pattern.matcher("AbcAbc");
		
		// attempt to find the next subsequence of the input sequence that matches the pattern
		matcher.find();
		System.out.println("'AbcAbc' matches 'A.*c' --> " + matcher.group());
		
		pattern = Pattern.compile("A.+");  // Greedy
		matcher = pattern.matcher("AbcAbc");
		
		// attempt to find the next subsequence of the input sequence that matches the pattern
		matcher.find();
		System.out.println("'AbcAbc' matches 'A.+' --> " + matcher.group());
		
		// Nongreedy quantifiers
		
		pattern = Pattern.compile("A.*?c");  // Non-Greedy
		matcher = pattern.matcher("AbcAbc");
		
		// attempt to find the next subsequence of the input sequence that matches the pattern
		matcher.find();
		System.out.println("'AbcAbc' matches 'A.*?c' --> " + matcher.group());

		pattern = Pattern.compile("A.+?");  // Non-Greedy
		matcher = pattern.matcher("AbcAbc");
		
		// attempt to find the next subsequence of the input sequence that matches the pattern
		matcher.find();
		System.out.println("'AbcAbc' matches 'A.+?' --> " + matcher.group());
		
	}
		
}

Output:

'AbcAbc' matches 'A.*c' --> AbcAbc
'AbcAbc' matches 'A.+' --> AbcAbc
'AbcAbc' matches 'A.*?c' --> Abc
'AbcAbc' matches 'A.+?' --> Ab

  
This was an example of how to use greedy and non-greedy regular expression matching in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button