
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Java Regular Expression That Doesn't Contain a Certain String
Example
import java.util.regex.*; class PatternMatch{ public static void main(String args[]) { String content = "I am a student"; String string = ".*boy.*"; boolean isMatch = Pattern.matches(string,content); System.out.println("The line contains 'boy'?"+ isMatch); } }
Output
the line contains 'boy'?false
matches()
It is used to check if the whole text matches a pattern. Its output is boolean. It returns true if match is found otherwise false.This is one of simplest and easiest way of searching a String in a text using Regex .There is a another method compile() , if you want to do a CASE INSENSITIVE search or want to do search multiple occurrences it can be used.
For above example it will be then −
String content = "I am a student"; String string = ".*BoY."; Pattern pattern = Pattern.compile(string, Pattern.CASE_INSENSITIVE);
Advertisements