zoukankan      html  css  js  c++  java
  • 正则表达式 匹配符合A表达式切不符合B表达式的字符串

    有一道这样的面试题

      写一个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

    正则表达式为:    (?!.*b)a

  • 相关阅读:
    「LibreOJ NOI Round #2」不等关系
    Atcoder Grand Contest 036 D
    「CTS2019」氪金手游
    「CTS2019」珍珠
    「APIO2016」烟花表演
    「PKUWC2018/PKUSC2018」试题选做
    「PKUWC2018」猎人杀
    「WC 2019」数树
    CodeForces 794 G.Replace All
    「BZOJ 4228」Tibbar的后花园
  • 原文地址:https://www.cnblogs.com/blog411032/p/8654810.html
Copyright © 2011-2022 走看看