- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Quantifiers in Java
Quantifier is a concept that allows the programmer to specify the number of occurrences of a specific type of value in the regular expression. There are different types of quantifiers, some of them include ‘?’ (Reluctant quantifier), ‘+’ (Possessive quantifier). In this post, we will see how reluctant quantifier works.
Example
Following is an example −
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { Pattern my_pattern = Pattern.compile("sam+?"); Matcher my_match = my_pattern.matcher("samp"); while (my_match.find()) System.out.println("The pattern has been found - " + my_match.start() + " to " + (my_match.end()-1)); } }
Output
The pattern has been found - 0 to 2
A class named Demo contains the main function. A pattern class instance is created, and a Matcher class instance is created to check if a match is found for a specific pattern. The ‘find’ function is used to check if a match has been found, and if this is true, relevant message is printed on the screen.
- Related Articles
- Greedy quantifiers Java Regular expressions in java.
- Regex quantifiers in Java Regular Expressions
- Types of quantifiers in Java regex
- Explain quantifiers in Java regular expressions
- Possessive quantifiers Java Regular expressions
- Reluctant quantifiers Java Regular expressions
- Quantifiers in C#
- Explain C# Quantifiers in regular expression
- Java Stream Collectors toCollection() in Java
- Java program to calculate mode in Java
- Convert Java String to Short in Java
- Compare Two Java int Arrays in Java
- Is Java matcher thread safe in Java?
- How to Sort a String in Java alphabetically in Java?
- Explain the architecture of Java Swing in Java?

Advertisements