Match a Line Not Containing a Word in Java Regex



Example

Live Demo

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NoRegTest {
   public static void main(String[] args) {
      String s="^fun";
      Pattern pattern = Pattern.compile(s);
      Matcher matcher = pattern.matcher("Java is fun");

      if(!matcher.find()) {
         System.out.println("not found");
      }
   }
}

Output

not found
Updated on: 2020-06-21T06:31:20+05:30

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements