Java Regular Expression that doesn't contain a certain String.

Java 8Object Oriented ProgrammingProgramming


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);


the line contains 'boy'?false


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);
Updated on 20-Jun-2020 10:34:58