In: Computer Science
Java:
Find a pattern that will match any string that is
-- at least 6 characters long,
-- and begins with a letter or number (\w)
-- and contains at least one non-letter and non-number (\W).
Question
Java:
Find a pattern that will match any string that is
-- at least 6 characters long,
-- and begins with a letter or number (w)
-- and contains at least one non-letter and non-number (W).
Program :
import java.util.regex.*;
import java.util.Scanner;
class RegexExample1{
public static void main(String args[]){
Scanner sc = new
Scanner(System.in);
System.out.println("Enter a string :
");
String content = sc.next();
String pattern = "(^w(?=.*W).{6,})";
boolean isMatch =
Pattern.matches(pattern, content);
System.out.println("Given string
matched to the regular expression" + isMatch);
}
}
OUTPUT :