
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Greedy quantifiers Java Regular expressions in java.
Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again. Following is the list of greedy quantifiers −
Quantifier | Description |
---|---|
re* | Matches zero or more occurrences. |
re? | Matches zero or, 1 occurrence. |
re+ | Matches one or more occurrences. |
re{n} | Matches exactly n occurrences. |
re{n, } | Matches at least n occurrences. |
re{n, m} | Matches at least n and at most m occurrences. |
Example
In the following Java example we are trying to match 1 or more digits, our input string is 45545 though the values 4, 45, 455 etc… are eligible, since we are using greedy quantifier it matches the longest possible.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[0-9]+"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); System.out.println(“”Matched text: ); while (matcher.find()) { System.out.println(matcher.group()); } } }
Output
Enter input text: Matched text: 45545
- Related Articles
- Possessive quantifiers Java Regular expressions
- Reluctant quantifiers Java Regular expressions
- Regex quantifiers in Java Regular Expressions
- Explain quantifiers in Java regular expressions
- A greedy qualifier in Java Regular Expressions
- Java Regular Expressions Tutorial
- Back references in Java regular expressions
- Regular Expressions syntax in Java Regex
- Pattern.matches() method in Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions
- Quantifiers in Java
- Java regular expressions sample examples
- Java Regular expressions Logical operators
- A Reluctant qualifier in Java Regular Expressions
- Use the ? quantifier in Java Regular Expressions

Advertisements