Java使用正则表达式判断字符串中是否包含某子字符串

需求:

给定一个字符串s,判断当s中包含“tree fiddy"或“3.50”或“three thirty”子字符串返回true,否则返回false.

题目来自代码勇士。


import java.util.regex.Pattern;

/**
 * 判断字符串中是否包含某字符串
 *
 * @author dylan
 * @create 2017-12-14
 **/
public class Nessie {
    public static boolean isLockNessMonster(String s) {
        return Pattern.matches(".*(tree fiddy|3\\.50|three fifty).*", s);
    }

    public static void main(String[] args) {
        System.out.println(isLockNessMonster("tree fiddy123"));
    }
}

输出:

true

posted @ 2017-12-14 23:12  一锤子技术员  阅读(24)  评论(0编辑  收藏  举报  来源