集册 Java实例教程 在正则表达式中使用否定

在正则表达式中使用否定

欢马劈雪     最近更新时间:2020-01-02 10:19:05

390
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
否定的开始是一个插入符号

import java.util.regex.Matcher;

import java.util.regex.Pattern;/**来自 时代Java公众号 - nowjava.com**/


public class Main {

  public static void main(String[] args) {

    Pattern pattern = Pattern.compile("[^cf]at");

    Matcher matcher = pattern.matcher("bat");

    if (matcher.matches())

      System.out.println("Match.");

    else

      System.out.println("Does not match.");

  }


}