Dowemo

Section 6 character group [ ] and [ ^ ]

Any character in the [ ] = = => match character set

A [ ^ ] = = => matches any character that isn't in the character set



System.out.println(Pattern.matches("[abcde]*", "abc") + "");


System.out.println(Pattern.matches("[abcde]*", "abcz") + "");


System.out.println(Pattern.matches("[^abcde]*", "abc") + "");


System.out.println(Pattern.matches("[^abcde]*", "fgh") + "");













true


false


false


true





















Section 7 - 连字符













在使用[] 或者[^]的过程中,有时候需要匹配的字符比较多,






那么,有没有比较简便的表达方式呢,有!






这个时候可以用-来连接字符,






代表从前一个字符到后一个字符之间的所有字符
















[a-z] ===>a到z的任意字符





















[a-z] ===>a到z的任意字符





















System.out.println(Pattern.matches("[A-C]*", "AB") + "");


System.out.println(Pattern.matches("[A-C]*", "ab") + "");







true
false


System.out.println(Pattern.matches("[a-c]*", "abc") + "");


System.out.println(Pattern.matches("[a-c]*", "abe") + "");







true
false

[0-9] ===>0到9的任意字符








System.out.println(Pattern.matches("[0-9]*", "12") + "");


System.out.println(Pattern.matches("[0-9]*", "1a") + "");























注意:只有在字符组内部,连字符才是元字符,并且如果元字符出现在字符组内部的开头,






则其也只能算作是一个普通字符















System.out.println(Pattern.matches("[-1-9]*", "12-") + "");









true


















Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs