Java学习 正则表达式详解

简介
正则表达式(英语:Regular expression,常简写为regex、regexp或RE),又称规律表达式、正则表示式、正则表示法、规则表达式、常规表示法,是计算机科学概念,用简单字符串来描述、匹配文中全部匹配指定格式的字符串,现在很多文本编辑器都支持用正则表达式搜索、取代匹配指定格式的字符串。

基本用法

基本使用:

package regex;

public class IntegerMatch {
    public static void main(String[] args) {
        /**
         * 要寻找一个数字可能有-号在最前面,表达式为 -?
         * 要描述一个整数,你可以使用\d表示一位数字
         * \\表示你想在正则表达式里插入一个普通的反斜线,如果你想插入的是数字\\d
         * 想要表达一个或多个之前的表达式应该使用+
         * 表示可能有一个负号,后面跟着多为数字的正则表达式为:-?\\d+
          */
        System.out.println("-648".matches("-?\\d+"));
        System.out.println("-10086".matches("-?\\d+"));
        System.out.println("10010".matches("-?\\d+"));
        System.out.println("+10016".matches("-?\\d+"));
        System.out.println("hello".matches("-?\\d+"));

    }
}

Output:

true
true
true
false
false

split()

String类还自带了正则表达式工具split()方法,功能是将字符串从正则表达式匹配位置切成字符数组:

package regex;
public class SpiltDemo {
    public static void main(String[] args) {
        String test="we should do something";
        String test1="hey,how are you?i am fine,and you?";

        System.out.println("test从空格处切开");
        //从空格处切开
        for (String s:test.split(" ")){
            System.out.println(s);
        }
        System.out.println("test1从非字符处切开");//除去英文字符大小写和数字之外的字符
        for (String s : test1.split("\\W+")) {
            System.out.println(s);
}



    }
}

Output:

test从空格处切开
we
should
do
something
test1从非字符处切开
hey
how
are
you
i
am
fine
and
you

replace()

String类还自带一个正则表达工具替换,你可以只替换正则表达式的第一个匹配子串,或替换所有匹配的地方:

package regex;

public class Replace {
    public static void main(String[] args) {
        String test="Google has many employees";
        //把e开头,后缀为词字符 替换成 items
        System.out.println(test.replaceAll("e\\w+","items"));
        System.out.println(test.replaceAll("Google|has|many","happy"));
        //第一个e结尾的字符串替换
        System.out.println(test.replaceFirst("\\w+e","Huawei"));

    }
}

Output:

Google has many items
happy happy happy employees
Huawei has many employees

创建正则表达式

参考JDK文档java.util.regex包中的Pattren类:

部分常用表达式

字符
B指定字符B
\xhhh十六进制为0xhh的字符
\uhhhh十六进制表示为0xhhhh的Unicode字符
\t制表符Tab
\n换行
\r回车
\f换页

\e

转义
 

字符类
[abc]包含a b c的任何字符
[^abc]除了 a b c的任何字符
[a-zA-Z]a-z 和A-Z任何字符
[a-d[m-p]]任意a-d m-p(合并)
[a-z&&[def]]任意d e f字符(交集)
\s空白符(空格 tab 回车 换页)
\S非空白符
\d数字[0-9]

\D

非数字

\w

词字符[a-zA-Z0-9]
\W非词字符


 

逻辑操作符
XYY在X后面
X|YX或Y
(X)捕获组。\i引用第i个捕获组

边界匹配符
^一行起始位
$一行结束
\b词的边界
\B非词的边界
\G前一个匹配的结束

例:

package regex;

public class match1 {
    public static void main(String[] args) {
        String test="Google";
        for(String pattern:new String[] {"Google","[gG]oogle","[Gg][a-z]+"}){
            System.out.println(test.matches(pattern));

        }
    }
}

Output:

true
true
true

Pattern和Matcher

范例:

package regex;
import java.util.regex.*;
public class PatternAndMatcher {
    public static void main(String[] args) {
        String re="\\bup\\b";
        String test="Look up Get up upper upon Climb up";
        /**Pattern.compile方法编译你的正则表达式**/
        Pattern pattern = Pattern.compile(re);
        /**将要检测到字符传入matcher方法生成Matcher对象**/
        Matcher matcher = pattern.matcher(test);
        int count=0;//记录test中匹配re的个数
        /**start和end方法显示匹配成功的起始和结束索引  前开后闭 **/
        while (matcher.find()){
            count++;
            System.out.print("Match number"+count+" ");
            System.out.print("start()"+matcher.start()+" ");
            System.out.println("end()"+matcher.end());
           
        }
    }

}

Output:

Match number1 start()5 end()7
Match number2 start()12 end()14
Match number3 start()32 end()34

 find()

此方法可用来在字符串中查找多个匹配:

package regex;

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

public class FindDemo {
    public static void main(String[] args) {
        Matcher m= Pattern.compile("\\w+")
                .matcher("There are moments that have a certain flavor of  eternity");
        while (m.find()) {
            System.out.print(m.group()+" ");//这里把字符串划分为单词

            }
        System.out.println();
        int i=0;//表示字符串中字符的位置,以其作为起点搜索
        while (m.find(i)) {
            System.out.print(m.group()+" ");
            i++;
        }
    }
}

Output:

There are moments that have a certain flavor of eternity 
There here ere re e are are re e moments moments oments ments ents nts ts s that that hat at t have have ave ve e a a certain certain ertain rtain tain ain in n flavor flavor lavor avor vor or r of of f eternity eternity eternity ternity ernity rnity nity ity ty y 

Groups(组)

用括号划分的正则表达式

A(B(C))D 有三个组 ABCD, BC,C

package regex;
import java.util.regex.*;
public class GroupDemo {
    public static void main(String[] args) {
        String test="This order was placed for QT3000! OK?";
        Matcher m=Pattern.compile("(\\D*)(\\d+)(.*)").matcher(test);
        while (m.find()) {
            for (int i = 0; i < m.groupCount();i++) {
                System.out.println("["+m.group(i)+"]");
}
        }
    }
}

Output:

[This order was placed for QT3000! OK?]
[This order was placed for QT]
[3000]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值