Java自学笔记之正则表达式

前言

  正则表达式是处理字符串匹配问题的工具,在爬虫和数据校验的时候用的比较多,下面介绍一下Java中正则表达式的若干规则。

语法

元字符 描述
.(点) 匹配除”\r\n”之外的任何单个字符。若要匹配包括”\r\n”在内的任意字符,请使用诸如”[\s\S]”之类的模式。
* 零次或多次匹配前面的字符或子表达式。例如,zo* 匹配”z”和”zoo”。* 等效于 {0,}。
+ 一次或多次匹配前面的字符或子表达式。例如,”zo+”与”zo”和”zoo”匹配,但与”z”不匹配。+ 等效于 {1,}。
? 当此字符紧随任何其他限定符(*、+、?、{n}、{n,}、{n,m})之后时,匹配模式是”非贪心的”。”非贪心的”模式匹配搜索到的、尽可能短的字符串,而默认的”贪心的”模式匹配搜索到的、尽可能长的字符串。例如,在字符串”oooo”中,”o+?”只匹配单个”o”,而”o+”匹配所有”o”。其它用法中表示零次或一次
?例子 *? 重复任意次,但尽可能少重复; +? 重复1次或更多次,但尽可能少重复; ?? 重复0次或1次,但尽可能少重复; {n,m}? 重复n到m次,但尽可能少重复; {n,}? 重复n次以上,但尽可能少重复
^ 匹配输入字符串开始的位置;还可以用到否定意义
$ 匹配输入字符串结尾的位置
{n} n 是非负整数。正好匹配 n 次。例如,”o{2}”与”Bob”中的”o”不匹配,但与”food”中的两个”o”匹配。
{n,} n 是非负整数。至少匹配 n 次。例如,”o{2,}”不匹配”Bob”中的”o”,而匹配”foooood”中的所有 o。”o{1,}”等效于”o+”。”o{0,}”等效于”o*”
{n,m} 匹配至少 n 次,至多 m 次。例如,”o{1,3}”匹配”fooooood”中的头三个 o。’o{0,1}’ 等效于 ‘o?’。注意:您不能将空格插入逗号和数字之间。
\d 数字字符匹配。等效于 [0-9]。
\D 非数字字符匹配。等效于 [^0-9]
\s 匹配任何空白字符,包括空格、制表符、换页符等。与 [ \f\n\r\t\v] 等效。
\S 匹配任何非空白字符。与 [^ \f\n\r\t\v] 等效
\b 匹配一个字边界,即字与空格间的位置。例如,”er\b”匹配”never”中的”er”,但不匹配”verb”中的”er”。
\B 非字边界匹配。”er\B”匹配”verb”中的”er”,但不匹配”never”中的”er”。
\w 匹配任何字类字符,包括下划线。与”[A-Za-z0-9_]”等效。
\W 与任何非单词字符匹配。与”[^A-Za-z0-9_]”等效。
[xyz] 字符集。匹配包含的任一字符。例如,”[abc]”匹配”plain”中的”a”。
[^xyz] 反向字符集。匹配未包含的任何字符。例如,”[^abc]”匹配”plain”中”p”,”l”,”i”,”n”。
[a-z] 字符范围。匹配指定范围内的任何字符。例如,”[a-z]”匹配”a”到”z”范围内的任何小写字母
[^a-z] 反向范围字符。匹配不在指定的范围内的任何字符。例如,”[^a-z]”匹配任何不在”a”到”z”范围内的任何字符。
(pattern) 匹配 pattern 并捕获该匹配的子表达式。可以使用 0 9 属性从结果”匹配”集合中检索捕获的匹配。若要匹配括号字符 ( ),请使用”(“或者”)”。
(?=pattern) 预测先行不占用字符,即发生匹配后,下一匹配的搜索紧随上一匹配之后,而不是在组成预测先行的字符后。

对小括号的解释的例子:
1、(abc|bcd|cde),表示这一段是abc、bcd、cde三者之一均可,顺序也必须一致
2、(abc)?,表示这一组要么一起出现,要么不出现,出现则按此组内的顺序出现
3、a(?=bbb) 顺序环视 表示a后面必须紧跟3个连续的b

程序测试练习

package com.spyder;

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

import org.junit.Test;

public class RegTest {

    @Test
    public void test1(){
        System.out.println("abc".matches("..."));  // true
        System.out.println("a8729a".replaceAll("\\d", "-")); // a----a
        Pattern p = Pattern.compile("[a-z]{3}");
        Matcher m = p.matcher("fgh");
        System.out.println(m.matches()); // true
        System.out.println("fgha".matches("[a-z]{3}")); // false
    }

    @Test
    public void test2(){
        System.out.println("a".matches("."));// true
        System.out.println("aa".matches("aa"));// true
        System.out.println("aaaa".matches("a*")); // true
        System.out.println("aaaa".matches("a+"));// true
        System.out.println("".matches("a*"));// true
        System.out.println("aaaa".matches("a?"));// false
        System.out.println("".matches("a?"));// true
        System.out.println("a".matches("a?"));// true
        System.out.println("214523145234532".matches("\\d{3,100}"));// true
        System.out.println("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));//false
        System.out.println("192".matches("[0-2][0-9][0-9]")); // true
    }

    @Test
    public void test3(){
        System.out.println("a".matches("[abc]")); // true
        System.out.println("a".matches("[^abc]")); // false
        System.out.println("A".matches("[a-zA-Z]"));// true
        System.out.println("A".matches("[a-z]|[A-Z]"));// true
        System.out.println("A".matches("[a-z[A-Z]]"));// true
        System.out.println("R".matches("[A-Z&&[RFG]]"));// true
    }

    @Test
    public void test4(){
        System.out.println(" \n\r\t".matches("\\s{4}"));// true
        System.out.println(" ".matches("\\S"));// false
        System.out.println("a_8".matches("\\w{3}"));// true
        System.out.println("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));// true
        System.out.println("\\".matches("\\\\"));// true
    }

    @Test
    public void test5(){
        System.out.println("hello sir".matches("^h.*"));// true
        System.out.println("hello sir".matches(".*ir$"));// true
        System.out.println("hello sir".matches("^h[a-z]{1,3}o\\b.*"));// true
        System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*")); // false
        System.out.println(" \n".matches("^[\\s&&[^\\n]]*\\n$"));// true
        System.out.println("aaa 8888c".matches(".*\\d{4}."));// true
        System.out.println("aaa 8888c".matches(".*\\b\\d{4}."));// true
        System.out.println("aaa8888c".matches(".*\\d{4}."));// true
        System.out.println("aaa8888c".matches(".*\\b\\d{4}."));// false
    }

    @Test
    public void test6(){
        Pattern p = Pattern.compile("\\d{3,5}");
        String s = "123-34345-234-00";
        Matcher m = p.matcher(s);
        System.out.println(m.matches()); // false
        m.reset();
        // reset:给当前的Matcher对象配上个新的目标,目标是就该方法的参数;如果不给参数,reset会把Matcher设到当前字符串的开始处。
        System.out.println(m.find());// true
        System.out.println(m.start() + "-" + m.end());// 0-3
        System.out.println(m.find());// true
        System.out.println(m.start() + "-" + m.end());// 4-9
        System.out.println(m.find());// true
        System.out.println(m.start() + "-" + m.end());// 10-13
        System.out.println(m.find());// false
        // find:部分匹配,从当前位置开始匹配,找到一个匹配的子串,将移动下次匹配的位置。
        // p(m.start() + "-" + m.end());
        System.out.println(m.lookingAt());// true
        System.out.println(m.lookingAt());// true
        System.out.println(m.lookingAt());// true
        System.out.println(m.lookingAt());// true
        // lookingAt 部分匹配,总是从第一个字符进行匹配,匹配成功了不再继续匹配,匹配失败了,也不继续匹配。
    }

    @Test
    public void test7(){
        Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
        String s = "123aa-34345bb-234cc-00";
        Matcher m = p.matcher(s);
        while(m.find()) {
            System.out.println(m.group());
            /**
             * 123aa
             * 34345bb
             * 234cc
             */
        }
    }

}

猜你喜欢

转载自blog.csdn.net/tsfx051435adsl/article/details/78805916