Regular expression matches multiple lines in Java

Java Programming Java8Java Technologies

Pattern.MULTILINE will help on matching multiple lines in java. below is an example to show this −

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

public class MultiRegex {

   public static void main(String[] args) {
      if (args.length<1) {
         System.err.println("run the program from command line");
         System.exit(1);
      }
      String regexString = args[0];
      String spamString = "welcome to java world";

      Pattern aPattern = Pattern.compile(regexString,Pattern.MULTILINE);
      Matcher aMatcher = aPattern.matcher(spamString);
      if (aMatcher.find()) {
         System.out.println("got a match");
      } else {
         System.out.println("no match");
      }
   }
}
raja
Published on 22-Jun-2018 15:40:05
Advertisements