本文,我们将来看一下几个可以用英文单词表达的正则表达式。这些可以使用的英文关键词,可以在类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/
相关推荐
本文写作时,一个包含了用正则表达式进行文本处理的Java规范需求(Specification Request)已经得到认可,你可以期待在JDK的下一版本中看到它。 然而,如果现在就需要使用正则表达式,又该怎么办呢?你可以从Apache...
正则表达式,正则表达式,正则表达式 正则表达式 正则表达式 正则表达式 正则表达式 正则表达式 正则表达式
常用正则表达式 正则表达式 常用正则表达式 正则表达式
正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式...
正则表达式.rar正则表达式.rar正则表达式.rar正则表达式.rar
《正则表达式经典实例》:每个程序员都会遇到需要使用正则表达式的情况,但是要用好正则表达式却并不容易。《正则表达式经典实例》提供了100多个实例,以帮助读者使用正则表达式处理数据和文本。即使有经验的用户也...
Java正则表达式Java正则表达式Java正则表达式Java正则表达式
正则表达式正则表达式正则表达式正则表达式 验证程序
正则表达式帮助 有用的正则表达式帮助 有用的正则表达式帮助 有用的正则表达式帮助 有用的
正则表达式验证工具 V1.0 本软件主要用于检测正则表达式是否正确。 运行环境:本软件为绿色软件,无需安装,但需要Microsoft .NET Framework 4 支持,如果没有请前去下载(下载路径:...
正则表达式日期校验 正则表达式日期校验 正则表达式日期校验
pb 使用正则表达式源码pbregexp pb中使用正则表达式组件,通过该组件可以在pb脚本中使用功能强大的正则表达式
《学习正则表达式》从正则表达式的基本概念讲起,到编写完整的sed和Perl脚本,再到转换HTML文件,将这种强大的工具解释得清晰透彻。...《学习正则表达式》适合对正则表达式感兴趣的程序员和互联网从业者。
正则表达式 正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式
正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式
正则表达式总结正则表达式总结正则表达式总结正则表达式总结正则表达式总结正则表达式总结
正则表达式速查 正则表达式举例 正则表达式学习 (4页A4纸)
正则表达式 详解 基础 正则表达式之道(A Tao of Regular Expressions)
UE正则表达式、UE正则表达式
常用正则表达式大全: 匹配中文字符的正则表达式 匹配空白行的正则表达式 匹配HTML标记的正则表达式 匹配Email地址的正则表达式 匹配网址URL的正则表达式 匹配国内电话号 匹配中国邮政编码 匹配身份证 匹配ip地址 ...