![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 find() to find a subsequence in Java Regexp
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 uses the find() method to find a subsequence 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("cool"); Matcher m = p.matcher("Java is cool"); System.out.println("Subsequence: cool"); System.out.println("Sequence: Java is cool"); if (m.find()) System.out.println("
Subsequence found"); else System.out.println("
Subsequence not found"); } }
Output
Subsequence: cool Sequence: Java is cool Subsequence found
Now let us understand the above program.
The subsequence “cool” is searched in the string sequence "Java is cool". 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("cool"); Matcher m = p.matcher("Java is cool"); System.out.println("Subsequence: cool" ); System.out.println("Sequence: Java is cool" ); if (m.find()) System.out.println("
Subsequence found"); else System.out.println("
Subsequence not found");
Advertisements