

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the subsequence in an input sequence that matches the pattern required in Java Regular Expressions?
The find() method finds the subsequence in an input sequence that matches the pattern required. This method is available in the Matcher class that is available in the java.util.regex package.
A program that demonstrates the method Matcher.find() in Java regular expressions 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("Sun"); Matcher m = p.matcher("The Earth revolves around the Sun"); System.out.println("Subsequence: Sun"); System.out.println("Sequence: The Earth revolves around the Sun"); if (m.find()) System.out.println("\nSubsequence found"); else System.out.println("\nSubsequence not found"); } }
Output
Subsequence: Sun Sequence: The Earth revolves around the Sun Subsequence found
Now let us understand the above program.
The subsequence “Sun” is searched in the string sequence "The Earth revolves around the Sun". Then the find() method is used to find if the subsequence is in the input sequence and the required result is printed. A code snippet which demonstrates this is as follows:
Pattern p = Pattern.compile("Sun"); Matcher m = p.matcher("The Earth revolves around the Sun"); System.out.println("Subsequence: Sun" ); System.out.println("Sequence: The Earth revolves around the Sun" ); if (m.find()) System.out.println("\nSubsequence found"); else System.out.println("\nSubsequence not found");
- Related Questions & Answers
- Getting the list of all the matches Java regular expressions
- Looping over matches with JavaScript regular expressions
- Use the ? quantifier in Java Regular Expressions
- Reset the Matcher in Java Regular Expressions
- How to find the column index in an R data frame that matches a condition?
- Which package is used for pattern matching with regular expressions in java?
- Pattern matches() method in Java with examples
- How to validate an email address using Java regular expressions.
- Java Regular Expressions Tutorial
- Regular expression matches multiple lines in Java
- Validate the ZIP code with Java Regular expressions
- Replacing all the matched contents Java regular expressions
- Pattern.matches() method in Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions
- Back references in Java regular expressions
Advertisements