文字列には、大文字と小文字を区別せずに複数の一致結果があります.

1669 ワード

テキストリンク:https://www.cnblogs.com/ccgjava/p/7214301.html
 // Java                      
        //   、//      
        String str = "Hello,my name is Ben.Please visit my website at http://www.forta.com/.";
        String regex = "My";
        Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        int count = 0;
        Matcher match = pattern.matcher(str);
        while (match.find()) {
            count++;
        }
        System.out.println(count);
        
        //   、//     
        String regex1 = "my";
        Pattern pattern1 = Pattern.compile(regex1);//     
        int count1 = 0;
        Matcher match1 = pattern1.matcher(str);
        while (match1.find()) {
            count1++;
        }
        System.out.println(count1);

        //   、
        String reg = "my";
        int count2 = 0;
        int index1 = 0;
        //indexOf         ,   String             。          ,    -1
        while ((index1 = str.indexOf(reg, index1)) != -1) {
            index1 = index1 + reg.length();
            count2++;
        }
        System.out.println(count2);


2
2
2
  String input = "I like Java,jAva is very easy and jaVa is so popular.";  
        String replacement="ccc";  
        System.out.println(input);  
        System.out.println(input.replaceAll("java", replacement));  
        System.out.println(input.replaceAll("(?i)java", replacement)); 


I like Java,jAva is very easy and jaVa is so popular.
I like Java,jAva is very easy and jaVa is so popular.
I like ccc,ccc is very easy and ccc is so popular.