- 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
Why we should use whole string in Java regular expression
In Java regex matches() matches the input string against the whole string as it add a ^ and $ at the end of the input string.so it will not match the substring. So for matching substring, you should use find().
Example
import java.util.regex.*; class PatternMatchingExample { public static void main(String args[]) { String content = "aabbcc"; String string = "aa"; Pattern p = Pattern.compile(string); Matcher m = p.matcher(content); System.out.println(" 'aa' Match:"+ m.matches()); System.out.println(" 'aa' Match:"+ m.find()); } }
Output
'aa' Match:false 'aa' Match:true
Advertisements