
- 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
Reluctant quantifiers Java Regular expressions
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.
Whereas a reluctant or, non-greedy quantifier matches as little as possible, initially the non-greedy quantifier matches the first character if match not occurred it adds another character from the input string and tries to match.
If you place a "?" after a greedy quantifier it becomes reluctant or non-greedy quantifier. Following is the list of reluctant 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
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); while (matcher.find()) { System.out.print("Pattern found from " + matcher.start()+ " to " + (matcher.end()-1)+"::"); System.out.print(matcher.group()); System.out.println(); } } }
Output
Enter input text: 12345678 Pattern found from 0 to 0::1 Pattern found from 1 to 1::2 Pattern found from 2 to 2::3 Pattern found from 3 to 3::4 Pattern found from 4 to 4::5 Pattern found from 5 to 5::6 Pattern found from 6 to 6::7 Pattern found from 7 to 7::8
- Related Articles
- Possessive quantifiers Java Regular expressions
- A Reluctant qualifier in Java Regular Expressions
- Regex quantifiers in Java Regular Expressions
- Explain quantifiers in Java regular expressions
- Greedy quantifiers Java Regular expressions in java.
- Java Regular Expressions Tutorial
- Explain C# Quantifiers in regular expression
- Java regular expressions sample examples
- Java Regular expressions Logical operators
- Validate Phone with Java Regular Expressions
- Back references in Java regular expressions
- Regular Expressions syntax in Java Regex
- Pattern.matches() method in Java Regular Expressions
- Name validation using Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions

Advertisements