有一道这样的面试题

  写一个Java方法,利用正则表达式判断输入str中包含字符串”ios“或”apple“(大小写不敏感),但不包括”mediaplayer“。如果满足条件,返回所包含的字符串”ios”和/或”apple“(按实际大小写返回)

 

解决办法:

package com.xfma.demo;

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

public class Demo {

    public static void main(String[] s) {
        test("iosjghjIOSfyfapplelioAPPLEbmediaplayer");
        test("iosjghjIOSfyfapplelioAPPLEb");
    }
    
    /***
     * 1.    写一个Java方法,利用正则表达式判断输入str中包含字符串”ios“或”apple“(大小写不敏感),
     * 但不包括”mediaplayer“。如果满足条件,返回所包含的字符串”ios”和/或”apple“(按实际大小写返回)
     * @param str
     */
    public static void test(String str) {
        Pattern pattern = Pattern.compile("(?!.*(mediaplayer))(ios|apple|IOS|APPLE)");
        Matcher matcher = pattern.matcher(str); 
        while(matcher.find()){ 
          System.out.println(matcher.group()); 
        } 
    }
}
View Code

相关文章:

  • 2021-05-24
  • 2021-12-30
  • 2021-11-21
  • 2022-12-23
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案