- 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
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);
- Related Articles
- Java Regular Expression to match a line that doesn't contain a word.
- Why milk doesn't contain Vitamin C?
- Check that the String does not contain certain characters in Java
- How to filter rows that contain a certain string in R?
- Java Program to split a string using Regular Expression
- Java Regular expression to check if a string contains alphabet
- How to Split String in Java using Regular Expression?
- Adding a column that doesn't exist in a query?
- Expression in CASE WHEN Clause doesn't work in MySQL query?
- Regular Expression "A" construct in Java
- Why we should use whole string in Java regular expression
- Program to match vowels in a string using regular expression in Java
- Regular Expression "(re)" Sub-Expression in Java
- How to match a regular expression against a string?
- Explain Regular Expression "A" Metacharacter in Java

Advertisements