COMMENTS 指定使用注释和忽略空白,也就是".*a"==". *a #this is comments"我想这个
* 在正则表达式很大,而且是在文件中输入时比较有用,平时我看也用不上。
*
* MULTILINE In multiline mode the expressions ^ and $ match just after
* or just before, respectively, a line terminator or the end of the
* input sequence. By default these expressions only match at the beginning
* and the end of the entire input sequence
* 指定使用多行匹配模式,在默认模式下,^和$分别只匹配一个输入的开始和结束。
* 在这种模式下,^和$ 除了匹配整个输入的开始和结束外还匹配一个line terminator的后边和
* 前边(不是前边和后边,就是说^匹配line terminator的后边$匹配line terminator的前边。
*
* DOATALL 如指定了这个模式则"."可匹配任何字符包括line terminator
*
* UNIX_LINES 指定这个模式时只有\n被认为是line terminator而\r和\r\n都不是
*
* 其他的我一时想不起来了,在具体介绍时再说吧。
*
*/
public class TestReg2
{
public static void main(String[] args)
{
String str1 = "";
Object str = "";
//注意:\r,\n,\b等转义字符在java字符串常量中要写成\\r,\\n,\\b等,否则编译都过不去
//\s匹配\r,\n,\r和空格
System.out.println("\\s匹配\\r,\\n,\\r和空格 "+" \t\n\r".matches("\\s{4}"));
//\S和\s互逆
System.out.println("\\S和\\s互逆 "+"/".matches("\\S"));
//.不匹配\r和\n
System.out.println(".不匹配\\r和\\n "+"\r".matches("."));
System.out.println("\n".matches("."));
//\w匹配字母,数字和下划线
System.out.println("\\w匹配字母,数字和下划线 "+"a8_".matches("\\w\\w\\w"));
//\W和\w互逆
System.out.println("\\W和\\w互逆 "+"&_".matches("\\W\\w"));
//\d匹配数字
System.out.println("\\d匹配数字 "+"8".matches("\\d"));
//\D与\d互逆
System.out.println("\\D与\\d互逆"+"%".matches("\\D"));
//两者都匹配但意文不同
System.out.println("======================");
System.out.println("表示\\000a匹配\\000a "+"\n".matches("\n"));
System.out.println("表示\\n匹配换行 "+"\n".matches("\\n"));
System.out.println("======================");
//两者都匹配但意文不同
System.out.println("\r".matches("\r"));
System.out.println("\r".matches("\\r"));
System.out.println("======================");
//^匹配开头
System.out.println("^匹配开头"+"hell".matches("^hell"));
System.out.println("abc\nhell".matches("^hell"));
//$匹配结束
System.out.println("$匹配结束"+"my car\nabc".matches(".*ar$"));
System.out.println("my car".matches(".*ar$"));
//\b匹配界
System.out.println("\\b匹配界 "+"bomb".matches("\\bbom."));
System.out.println("bomb".matches(".*mb\\b"));
//\B与\b互逆
System.out.println("\\B与\\b互逆"+"abc".matches("\\Babc"));
//[a-z]匹配a到z的小写字母
System.out.println("[a-z]匹配a到z的小写字母"+"s".matches("[a-z]"));
System.out.println("S".matches("[A-Z]"));
System.out.println("9".matches("[0-9]"));
//取反
System.out.println("取反"+"s".matches("[^a-z]"));
System.out.println("S".matches("[^A-Z]"));
System.out.println("9".matches("[^0-9]"));
//括号的作用
System.out.println("括号的作用"+"aB9".matches("[a-z][A-Z][0-9]"));
System.out.println("aB9bC6".matches("([a-z][A-Z][0-9])+"));
//或运算
System.out.println("或运算"+"two".matches("two|to|2"));
System.out.println("to".matches("two|to|2"));
System.out.println("2".matches("two|to|2"));
//[a-zA-z]==[a-z]|[A-Z]
System.out.println("[a-zA-z]==[a-z]|[A-Z]"+"a".matches("[a-zA-Z]"));
System.out.println("A".matches("[a-zA-Z]"));
System.out.println("a".matches("[a-z]|[A-Z]"));
System.out.println("A".matches("[a-z]|[A-Z]"));
//体会一下以下四个
System.out.println("体会一下以下四个\n==========================");
System.out.println(")".matches("[a-zA-Z)]"));
System.out.println(")".matches("[a-zA-Z)_-]"));
System.out.println("_".matches("[a-zA-Z)_-]"));
System