- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Use a quantifier to find a match in Java
One of the quantifiers is the plus(+). This matches one or more of the subsequence specified with the sequence.
A program that demonstrates using the quantifier plus(+) to find a match in Java is given as follows:
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("o+"); Matcher m = p.matcher("o oo ooo"); System.out.println("The input string is: o oo ooo"); System.out.println("The Regex is: o+ "); System.out.println(); while (m.find()) { System.out.println("Match: " + m.group()); } } }
Output
The input string is: o oo ooo The Regex is: o+ Match: o Match: oo Match: ooo
Now let us understand the above program.
The subsequence “o+” is searched in the string sequence "o oo ooo". Then the find() method is used to find if the subsequence i.e. o followed by any number of o is in the input sequence and the required result is printed. A code snippet which demonstrates this is as follows:
Pattern p = Pattern.compile("o+"); Matcher m = p.matcher("o oo ooo"); System.out.println("The input string is: o oo ooo"); System.out.println("The Regex is: o+ "); System.out.println(); while (m.find()) { System.out.println("Match: " + m.group()); }
- Related Articles
- Use the ? quantifier in Java Regular Expressions
- Use Pattern class to match in Java
- How to use regular expression in Java to pattern match?
- Use find() to find a subsequence in Java Regexp
- Finding a Match Within Another Match Java regular expressions
- Split a string around a particular match in Java
- How to match a line not containing a word in Java Regex
- Match all occurrences of a regex in Java
- How to match a particular word in a string using Pattern class in Java?
- Use find() to find multiple subsequences in Java
- Program to match vowels in a string using regular expression in Java
- How do we use Python regular expression to match a date string?
- MySQL query to find a match and fetch records
- How to match a range of characters using Java regex
- How to match a non-word character using Java RegEx?

Advertisements