Regex matching beginning AND end strings

前端 未结 5 537
长情又很酷
长情又很酷 2020-12-05 06:50

This seems like it should be trivial, but I\'m not so good with regular expressions, and this doesn\'t seem to be easy to Google.

I need a regex that starts with the

5条回答
  •  鱼传尺愫
    2020-12-05 07:12

    Scanner scanner = new Scanner(System.in);
    String part = scanner.nextLine();
    String line = scanner.nextLine();
    
    String temp = "\\b" + part +"|"+ part + "\\b";
    Pattern pattern = Pattern.compile(temp.toLowerCase());
    Matcher matcher = pattern.matcher(line.toLowerCase());
    
    System.out.println(matcher.find() ? "YES":"NO");
    

    If you need to determine if any of the words of this text start or end with the sequence. you can use this regex \bsubstring|substring\b anythingsubstring substringanything anythingsubstringanything

提交回复
热议问题