Patterns and Matcher In the middle of a word

The first line of the input contains a sequence of letters.

The second line of the input contains some text.

Your task is to determine if any of these words contains this sequence of letters in the middle. The sequence cannot be located at the start or at the end of the word, only in the middle. If there is such a word, you should output "YES", otherwise output "NO". A word can contain only symbols of the English alphabet. You should ignore the case while searching for matches.

Sample Input 1:

Gramm
Java is the most popular programming language

Sample Output 1:

YES

Sample Input 2:

Press
Regular expressions is hard to read, isnt it?

Sample Output 2:

YES

Sample Input 3:

some
Wow! How awesome is that!

Sample Output 3:

NO
import java.util.*;
import java.util.regex.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String part = scanner.nextLine();
        String line = scanner.nextLine();
        Pattern pattern = Pattern.compile("\\B" + part + "\\B", Pattern.CASE_INSENSITIVE);
        System.out.println(pattern.matcher(line).find() ? "YES" : "NO");
    }
}
posted @ 2020-08-21 13:24  longlong6296  阅读(165)  评论(0)    收藏  举报