集册 Java实例教程 确定字符串是否与模式完全匹配

确定字符串是否与模式完全匹配

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

475
确定字符串是否与模式完全匹配

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main {/** 来自 n o w j a v a . c o m**/


  public void main(String[] argv) {

    String patternStr = "b";

    Pattern pattern = Pattern.compile(patternStr);


    // Determine if there is an exact match

    CharSequence inputStr = "a b c";

    Matcher matcher = pattern.matcher(inputStr);

    boolean matchFound = matcher.matches(); // false


    // Try a different input

    matcher.reset("b");

    matchFound = matcher.matches(); // true


    
展开阅读全文