几个可以用英文单词表达的正则表达式

17 篇文章 0 订阅

本文,我们将来看一下几个可以用英文单词表达的正则表达式。这些可以使用的英文关键词,可以在类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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值