码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA中的正则表达式--待续

时间:2015-07-08 00:24:39      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

1、关于“\”,在JAVA中的正则表达式中的不同;

    在其他语言中"\\"表示为:我想要在正则表达式中插入一个普通的反斜杠;

    在Java中“\\”表示为:我想要插入一个正则表达式反斜杠;

    eg:验证整数的正则表达式为\\d; 如果想要插入一个反斜杠则为:\\\\ ;

    如果是换行符和制表符则为\n 和\t ;

2、特殊字符

 

Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n
X{n,} X,至少 n
X{n,m} X,至少 n 次,但是不超过 m
 
Reluctant 数量词
X?? X,一次或一次也没有
X*? X,零次或多次
X+? X,一次或多次
X{n}? X,恰好 n
X{n,}? X,至少 n
X{n,m}? X,至少 n 次,但是不超过 m
 
Possessive 数量词
X?+ X,一次或一次也没有
X*+ X,零次或多次
X++ X,一次或多次
X{n}+ X,恰好 n
X{n,}+ X,至少 n
X{n,m}+ X,至少 n 次,但是不超过 m

演示代码:

package com.st.day20150525;

public class StringTest03 {
    
    public static String message ="The class String includes methods for examining "
            + "individual characters of the sequence, for comparing strings, "
            + "for searching strings, for extracting substrings, and for "
            + "creating a copy of a string with all characters translated to "
            + "uppercase or to lowercase. Case mapping is based on the Unicode "
            + "Standard version specified by the Character class.";
    
    public static void main(String[] args) {
        //1、使用String中的内建的功能,进行字符的严重
        System.out.println("1243".matches("-?\\d+"));
        System.out.println("+911".matches("-?\\d+"));
        System.out.println("+911".matches("(-|\\+)?\\d+"));
        
        //2、使用String.split() 将字符串从正则表达式匹配的地方分开
        String[] str01 = message.split("of|to");    // 从of 或者 to 的地方进行分开
        for (int i = 0; i < str01.length; i++) {
            System.out.println("第"+i+": "+str01[i]);
        }
        String[] str02 = message.split("\\W");     // 从空格的地方进行分开
        for (int i = 0; i < str02.length; i++) {
            System.out.print("第"+i+": "+str02[i]+"  ");
        }
    }
}

 

JAVA中的正则表达式--待续

标签:

原文地址:http://www.cnblogs.com/xiaotao726/p/4628896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!