- 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
A greedy qualifier in Java Regular Expressions
A greedy qualifier repeats the specified token as many times as possible and then the engine backtracks and the greedy qualifier gives up matches to eventually find the required match.
The regex "(\w+)(\d)(\w+)" is used to find the match in the string "EarthHas1Moon".
A program that demonstrates this is given as follows:
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { String str = "EarthHas1Moon"; String regex = "(\w+)(\d)(\w+)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); m.find(); System.out.println(m.group(1)); System.out.println(m.group(2)); System.out.println(m.group(3)); } }
Output
EarthHas 1 Moon
Now let us understand the above program.
The regex is “(\w+)(\d)(\w+)”. This is searched in the string sequence "EarthHas1Moon". The find() method is used to find if the regex is in the input sequence and the required result is printed. A code snippet which demonstrates this is as follows:
String str = "EarthHas1Moon"; String regex = "(\w+)(\d)(\w+)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); m.find(); System.out.println(m.group(1)); System.out.println(m.group(2)); System.out.println(m.group(3));
- Related Articles
- A Reluctant qualifier in Java Regular Expressions
- Greedy quantifiers Java Regular expressions in java.
- Java Regular Expressions Tutorial
- Back references in Java regular expressions
- Regular Expressions syntax in Java Regex
- Regex quantifiers in Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions
- Pattern.matches() method in Java Regular Expressions
- PatternSyntaxException class in Java regular expressions
- Explain quantifiers in Java regular expressions
- Use a character class in Java Regular Expressions
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions
- Java Regular expressions Logical operators
- Reluctant quantifiers Java Regular expressions

Advertisements