- 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
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
- Related Articles
- Why do we use re.compile() method in Python regular expression?
- Why do we use question mark literal in Python regular expression?
- Why should we use a StringBuffer instead of a String in Java?\n
- How do we use a delimiter to split string in Python regular expression?
- How do we use Python regular expression to match a date string?
- How do we use re.finditer() method in Python regular expression?
- How do we use Python Regular Expression named groups?
- Why should we use element in JavaScript?
- Why we should use set.seed in R?
- How to Split String in Java using Regular Expression?
- Why should we not use ++, -- operators in JavaScript?
- Why should we use MySQL CASE Statement?
- How to use regular expression in Java to pattern match?
- Java Program to split a string using Regular Expression
- Regular Expression "(re)" Sub-Expression in Java

Advertisements