码迷,mamicode.com
首页 > 其他好文 > 详细

正则表达式

时间:2014-09-28 01:51:00      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   java   

申明:正则表达式可以大大的简化代码,不过对于看不懂的人来说,那也只能骂娘。切身体会,所以感觉有必要撸下来正则表达式!(后期会不断添加各种正则判断)

基本概念不多说我,直接上例子,通过例子说明吧。

一、正则基础

Demo1:

在java中对反斜线\的处理与其它语言不同,在其它语言中,\\表示“我想要在正则表达式中插入一个普通的(字面上的)反斜线”,请不要给它任何特殊的意义。而在java中,\\的意思是“我要插入一个正则表达式的反斜线,所以其后的写符具有特殊的意义。"例如,如果我想要表示一位数字,那么正则表达式应该是\\d。如果你想插入一个普通的反斜线,则应该这样\\\\,不过换行和制表之类的东西只需要使用单反斜线:\n\t

package com.rah;
/***
 * 
 * @author team
 *
 */
public class Demo1 {

    public static void main(String[] args) {
        /***
         * 反斜线\在程序中必须以\\表示如下:
         */
        System.out.println("\\".matches("\\\\"));
        /***
         * ("-?\\d+")匹配:可能有一个负号,或者后面跟着一位或多位数字
         */
        System.out.println("-1234".matches("-?\\d+"));
        System.out.println("5678".matches("-?\\d+"));
        System.out.println("+911".matches("-?\\d+"));
        /***
         * ("(-|\\+)?\\d+")匹配:表示字符串的起始字符可能是一个-或者+(+有特殊意义,需要用\\转义),后面跟着一位或多位数字
         */
        System.out.println("+911".matches("(-|\\+)?\\d+"));
    }

}

运行结果:

true
true
true
false
true

Demo2
String.spilt是一个非常有用的正则表达式工具,其功能是”将字符串从正则表达式匹配的地方切开“。

package com.rah;

import java.util.Arrays;

/***
 * 
 * @author team
 * 
 */
public class Demo2 {
    public static String knights = "Then, when you have found the shrubbery, you must "
            + "cut down the mightiest tree in the forest..."
            + "with... a herring!";

    public static void split(String regex) {
        System.out.println(Arrays.toString(knights.split(regex)));
    }

    public static void main(String[] args) {
        /***
         * 按空格划分字符串
         */
        split(" ");
        /***
         * \W(\\W转义)意思是非单词字符如果是小写W,\w则表示一个单词字符
         * 该正则可以标点字符给删了
         */
        split("\\W+");
        /***
         * 字母n后面跟着一个单词字符
         */
        split("n\\W+");
    }
}

运行结果:

[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest...with..., a, herring!]
[Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring]
[The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest...with... a herring!]

Demo3

String.replaceFirst()/replaceAll(),也是可以匹配正则的

package com.rah;
/***
 * 
 * @author team
 * 
 */
public class Demo3 {
    public static String  sqlOne = "select * from students";
    public static String  sqlTwo = "seelct count(*) from students";
    public static void main(String[] args) {
        /***
         * 从前四个输出可以看出[]里面是只匹配他就会在第一时间匹配到就不会往下找,就是因为这个小知识点,在开发中浪费了我好多时间Q_Q
         */
        System.out.println(sqlOne.replaceFirst("s", "count(*)"));
        System.out.println(sqlOne.replaceFirst("[s]", "count(*)"));
        /***
         * 找到se匹配
         */
        System.out.println(sqlOne.replaceFirst("se", "count(*)"));
        /***
         * 找到s匹配,就不会往下找
         */
        System.out.println(sqlOne.replaceFirst("[se]", "count(*)"));
        
        System.out.println(sqlOne.replaceFirst("[*]", "count(*)"));
        System.out.println(sqlTwo.replaceFirst("count\\(\\*\\)", "*"));
    }
}

运行结果:

count(*)elect * from students
count(*)elect * from students
count(*)lect * from students
count(*)elect * from students
select count(*) from students
seelct * from students

Demo4:

检查句子以大写字母开头、以句号结尾

package com.rah;
/***
 * 
 * @author team
 *
 */
public class Demo4 {
    public static boolean matches(String text) {
        /***
         * \\p{javaUpperCase} 大写字母,不明白的可以看jdk文档
         */
        return text.matches("\\p{javaUpperCase}.*\\.");
    }
    public static void main(String[] args) {
        System.out.println(matches("This is correct."));
        System.out.println(matches("bad sentence 1."));
        System.out.println(matches("Bad sentence 2"));
        System.out.println(matches("This is also correct..."));
    }
}

运行结果:

true
false
false
true

Demo5:

package com.rah;

import java.util.Arrays;

/***
 * 
 * @author team
 * 
 */
public class Demo5 {
    public static String knights = "Then, when you have found the shrubbery, you must "
            + "cut down the mightiest tree in the forest..."
            + "with... a herring!";

    public static void split(String regex) {
        System.out.println(Arrays.toString(knights.split(regex)));
    }

    public static void main(String[] args) {
        /***
         * 在the和you处分割
         */
        split("the|you");
    }
}

运行结果:

[Then, when ,  have found ,  shrubbery, ,  must cut down ,  mightiest tree in ,  forest...with... a herring!]

Demo6

package com.rah;

/***
 * 
 * @author team
 * 
 */
public class Demo5 {
    public static String knights = "Then, when you have found the shrubbery, you must "
            + "cut down the mightiest tree in the forest..."
            + "with... a herring!";

    /*
     * 对应的内嵌标志表达式是 (?i),它有四种形式:
     *  1,(?i) 
     *  2,(?-i) 
     *  3,(?i:X) 
     *  4,(?-i:X) 
     *  不带有 - 的是开标志,带有 - 的是关标志。
     */
    public static void main(String[] args) {
        /***
         * 对book都忽略大写
         */
        System.out.println("Book".matches("(?i)Book"));
        /***
         * 对b都忽略大写,ook还是得比较大小写,下面的方法作用一样,写的更简洁
         */
        System.out.println("Book".matches("(?i)b(?-i)ook"));
        /***
         * 对b都忽略大写,ook还是得比较大小写
         */
        System.out.println("Book".matches("(?i:b)ook"));

        /***
         * (?-i) 的作用域是前面,如a(?-i) (?-i)的作用域是后面,如(?i)B
         */
        System.out.println("bOOk".matches("b(?-i)(?i)ook"));
        System.out.println("aBook".matches("a(?-i:B)ook"));
        
        /***
         * [] 只要匹配到一个再往下匹配,匹配不到了就把当前的替换
         * 没有[] 他要满足字符串到匹配到才换
         */
        System.out.println("ouahoahuah".replaceAll("[ou]", ""));
        System.out.println("ouahoahuah".replaceAll("ou", ""));
        /***
         * 忽略大小写匹配aeiou
         */
        System.out.println(knights.replaceAll("(?i)[aeiou]", ""));
    }
}

运行结果:

true
true
true
true
true
ahahah
ahoahuah
Thn, whn y hv fnd th shrbbry, y mst ct dwn th mghtst tr n th frst...wth...  hrrng!

二、创建正则表达式

写法参考java.util.regex包下的Pattern类

Demo7:

package com.rah;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo7 {
    public static void main(String[] args) {
        if(args.length<2){
            System.out.println("Usage:\njava TestRegularExpression " + 
                             "characterSequence regularExpression");
            System.exit(0);
        }
        System.out.println("Input: \"" + args[0] + "\"");
        for(String arg : args){
            System.out.println("Regulqr expression: \"" +arg +"\"");
            Pattern p = Pattern.compile(arg);
            Matcher m = p.matcher(args[0]);
            while(m.find()){
                System.out.println("Match \"" + m.group() + "\" at position " + m.start() + "-" + (m.end()-1));
            }
        }
    }
}

传入的参数:

abcabcabcdefabc abc+ (abc)+ (abc){2,}

运行结果:

Input: "abcabcabcdefabc"
Regulqr expression: "abcabcabcdefabc"
Match "abcabcabcdefabc" at position 0-14
Regulqr expression: "abc+"
Match "abc" at position 0-2
Match "abc" at position 3-5
Match "abc" at position 6-8
Match "abc" at position 12-14
Regulqr expression: "(abc)+"
Match "abcabcabc" at position 0-8
Match "abc" at position 12-14
Regulqr expression: "(abc){2,}"
Match "abcabcabc" at position 0-8

 未完待续。。

正则表达式

标签:style   blog   http   color   io   os   使用   ar   java   

原文地址:http://www.cnblogs.com/rah123/p/3997531.html

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