本文,我们将来看一下几个可以用英文单词表达的正则表达式。这些可以使用的英文关键词,可以在类java.util.regex.Pattern找到,如下:
public final class Pattern
implements java.io.Serializable
{ ? // Posix regular expression character classes, defined in
???????????? // http://www.unix.org/onlinepubs/009695399/basedefs/xbd_chap09.html
???????????? defRange( "ASCII" , 0x00 , 0x7F );?? // ASCII
???????????? defCtype( "Alnum" , ASCII.ALNUM);? // Alphanumeric characters
???????????? defCtype( "Alpha" , ASCII.ALPHA);? // Alphabetic characters
???????????? defCtype( "Blank" , ASCII.BLANK);? // Space and tab characters
???????????? defCtype( "Cntrl" , ASCII.CNTRL);? // Control characters
???????????? defRange( "Digit" , ‘0‘ , ‘9‘ );???? // Numeric characters
???????????? defCtype( "Graph" , ASCII.GRAPH);? // printable and visible
???????????? defRange( "Lower" , ‘a‘ , ‘z‘ );???? // Lower-case alphabetic
???????????? defRange( "Print" , 0x20 , 0x7E );?? // Printable characters
???????????? defCtype( "Punct" , ASCII.PUNCT);? // Punctuation characters
???????????? defCtype( "Space" , ASCII.SPACE);? // Space characters
???????????? defRange( "Upper" , ‘A‘ , ‘Z‘ );???? // Upper-case alphabetic
???????????? defCtype( "XDigit" ,ASCII.XDIGIT); // hexadecimal digits
} |
下面我们就来看几个例子吧.
判断一个字符串是否只包含字母
我们可以使用的正则表达式是^\\p{Alpha}+$
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? String asciiRegex = "^\\p{Alpha}+$" ;
???????? System.out.println( "QWEabgjgjagdg" .matches(asciiRegex)); // true
???????? System.out.println( "Hello Java" .matches(asciiRegex)); // false
???????? System.out.println( "$abc123" .matches(asciiRegex)); // false
???? }
?
} |
判断一个字符串是否只包含0到127的ASCII
我们可以使用的正则表达式是^\\p{ASCII}+$
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? // 使用^\\p{ASCII}+$判断一个字符串是否只包含0到127的ASCII
???????? String asciiRegex = "^\\p{ASCII}+$" ;
???????? System.out.println( "@#$%^&QWERTTYYzcssdfsd1233534 234242"
???????????????? .matches(asciiRegex)); // true
???????? System.out.println( "Hello Java ¥" .matches(asciiRegex)); // false
???? }
?
} |
判断一个字符串是否只包含数字
我们可以使用的正则表达式是^\\p{Digit}+$
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? String asciiRegex = "^\\p{Digit}+$" ;
???????? System.out.println( "123" .matches(asciiRegex)); // true
???????? System.out.println( "Hello Java" .matches(asciiRegex)); // false
???????? System.out.println( "$abc123" .matches(asciiRegex)); // false
???? }
?
} |
判断一个字符串是否只包含小写字母
我们可以使用的正则表达式是^\\p{Lower}+$
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? String asciiRegex = "^\\p{Lower}+$" ;
???????? System.out.println( "abc" .matches(asciiRegex)); // true
???????? System.out.println( "HelloJava" .matches(asciiRegex)); // false
???????? System.out.println( "$abc" .matches(asciiRegex)); // false
???? }
} |
判断一个字符串是否只包含大写字母
我们可以使用的正则表达式是^\\p{Upper}+$
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? String asciiRegex = "^\\p{Upper}+$" ;
???????? System.out.println( "ABC" .matches(asciiRegex)); // true
???????? System.out.println( "HelloJava" .matches(asciiRegex)); // false
???????? System.out.println( "$abc" .matches(asciiRegex)); // false
???? }
} |
判断一个字符串是否只包含空格
我们可以使用的正则表达式是^\\p{Space}+$
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? String asciiRegex = "^\\p{Space}+$" ;
???????? System.out.println( "?? " .matches(asciiRegex)); // true
???????? System.out.println( "JAVA JAVA" .matches(asciiRegex)); // false
???????? System.out.println( " $ B" .matches(asciiRegex)); // false
???? }
} |
判断一个字符串是否只包含标点符号
我们可以使用的正则表达式是^\\p{Punct}+$
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? String asciiRegex = "^\\p{Punct}+$" ;
???????? System.out.println( ".,!" .matches(asciiRegex)); // true
???????? System.out.println( ".,q" .matches(asciiRegex)); // false
???????? System.out.println( " ," .matches(asciiRegex)); // false
???? }
} |
判断一个字符串是否只包数字和字母
我们可以使用的正则表达式是^\\p{Alnum}+$
public class RegularExpressionExample {
?
public class RegularExpressionExample {
?
???? public static void main(String[] args) {
?
???????? String asciiRegex = "^\\p{Alnum}+$" ;
???????? System.out.println( "QWE123456" .matches(asciiRegex)); // true
???????? System.out.println( "Hello Java" .matches(asciiRegex)); // false
???????? System.out.println( "$abc123" .matches(asciiRegex)); // false
???? }
} |
其余的几个有兴趣的朋友可以去尝试一下,这里就不再一一介绍了。
?
原文地址:http://thecodesample.com/?p=981
更多例子请访问http://thecodesample.com/
原文:http://mouselearnjava.iteye.com/blog/2158849