集册 Java实例教程 在正则表达式中设置区分大小写

在正则表达式中设置区分大小写

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

539
在正则表达式中设置区分大小写

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main {
/*
时代Java 提 供
*/


  public void m() {

    CharSequence inputStr = "Abc";

    String patternStr = "abc";


    // Compile with case-insensitivity

    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);

    Matcher matcher = pattern.matcher(inputStr);

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


    // Use an inline modifier

    matchFound = pattern.matches("abc", "aBc"); // false

    matchFound = pattern.matches("(?i)abc", "aBc"); // true// from n o w j a v a . c o m - 时  代  Java

    matchFound = pattern.matches("a(?i)bc", "aBc"); // true


    // Use enclosing form

    matchFound = pattern.matches("((?i)a)bc", "aBc"); // false

    matchFound = pattern.matches("(?i:a)bc", "aBc"); // false

    matchFound = pattern.matches("a((?i)b)c", "aBc"); // true

    matchFound = pattern.matches("a(?i:b)c", "aBc"); // true


 
展开阅读全文