Java中的正则表达式

正则表达式(regular expression)是指一种操作字符串的搜索模式,可用于文本的搜索、编辑等操作。如下是一个正则表达式:

^[0-9]

其定义了一个搜索非数字字符的模式。

正则表达式规则

字符匹配符

下表是常用的字符匹配符:

表1 字符匹配符

正则表达式说明
.匹配任意字符
^regex在一行的开始出匹配regex
regex$在一行的末尾匹配regex
[abc]匹配字符a/b/c
[abc][vz]匹配a/b/c后跟v/z的字符串
[^abc]匹配除了a/b/c之外的任意字符
[a-d]匹配a到d之间的字符
[0-8]匹配0到8之间的数字
XZ搜索 XZ
x\z搜索 X 或者 Z
$是否一行的末尾

注意,由于Markdown编辑器缘故,x\z实际是 x|z

元字符

为了简化表达式规则,正则表达式提供了几种元字符(meta characters):

表2 元字符

正则表达式说明
\d任意数字,[0-9]的简写
\D非数字匹配,[^0-9]的简写
\s空白字符,[ \t\n\x0b\r\f]的简写
\S非空白字符,[^\s]的简写
\w单词匹配符,[a-zA-Z_0-9]的简写
\W非单词匹配符,[^\w]的简写
\S+多个非空白字符
\b单词[a-zA-Z0-9_]边界字符匹配

数量匹配符

另外,正则表达式还提供了数量匹配符(Quantifier),用于标识一个元素出现的频次,主要有以下几种:

表3 数量匹配符

正则表达式说明示例
*出现次数 >= 0,等同{0,}x* 查找零个或者多个字符x; .*匹配任意字符串
+出现次数 >= 1,等同 {1,}x+ 匹配出现次数大于1的字符x
?出现次数不多于1次,等同{0,1}x? 查找出现次数不大于1次的字符x
{n}出现次数为 n\d{3} 搜索长度为3的数字字符串
{n1,n2}出现次数在 n1 与 n2 之间\d{1,4} 数字字符长度在1 ~ 4之间的字符串
*??放在一个数量匹配符的后面时,定义为一个“懒惰数量匹配符” (reluctant quantifier),该匹配符找到最小的一个匹配,然后搜索到第一个匹配字符时,即不再搜索

给定正则表达式的模式

可以在一个正则表达式的开始给定一个模式修改符(mode modifiers):

  • (?i) 使正则表达式不区分大小写
  • (?s) 单行模式,匹配包括换行符在内的所有字符(makes the dot match all characters, including line breaks)
  • (?m) 多行模式(makes the caret and dollar match at the start and end of each line in the subject string.)

若需要指定多种模式,则将其组合在一起即可: (?ism)

在Java中使用正则表达式

Java中的 String 支持正则表达式来操作字符串,这给文本操作带来了很大的方便:

表4 String中的正则表达式方法

方法说明
str.matches("regex")判断字符串str是否与regex相匹配
str.split("regex")通过regex分割字符串str
str.replaceFirst("regex","replacement")replacement替换字符串中第一次出现 regex的字符串
str.replaceAll("regex","replacement")用于replacement替换所有匹配regex的字符串

参考示例:

    package de.vogella.regex.test;

    public class RegexTestStrings {
        public static final String EXAMPLE_TEST = "This is my small example "
        + "string which I'm going to " + "use for pattern matching.";

        public static void main(String[] args) {
        System.out.println(EXAMPLE_TEST.matches("\\w.*"));
        String[] splitString = (EXAMPLE_TEST.split("\\s+"));
        System.out.println(splitString.length);// should be 14
        for (String string : splitString) {
          System.out.println(string);
        }
        // replace all whitespace with tabs
        System.out.println(EXAMPLE_TEST.replaceAll("\\s+", "\t"));
      }
    } 

注意: 在Java中斜杠\是一个转义字符,因此为了得到单个的斜杠字符,需要用\\来实现

Pattern/Matcher

对于更高级的应用,Java提供了两个类Pattern(java.util.regex.Pattern) 和 Matcher (java.util.regex.Matcher):

  1. 首先,使用 Pattern 得到对应的正则表达式;
  2. 然后,利用 Matcher 来操作相应的字符串

参考示例:

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

    public class RegularExpression {
        private String regx = null;
        private Pattern pattern = null;
        private Matcher matcher = null;

        public static void main(String[] args){     
            // regular expression test
            String input =  new String("here 2016, now we encounter very confusing things. On the one hand, we human beings feel "
                    + "very confident, but on the other hand, we are so fucking lost in our self-built world! we do waste our energy"
                    + "and time  on useless things. We are totally lost.");

            String regDigit = new String("\\d");
            String regChars = new String("hand|useless");
            String regWild = new String("[^af]");
            String regWord = new String("\\w");
            String regTimes = new String("[a-z]{5}");

            RegularExpression reg = new RegularExpression(regDigit);
            reg.getMatcherResult(input);

            reg.setRegx(regChars);
            reg.getMatcherResult(input);

            reg.setRegx(regWild);
            reg.getMatcherResult(input);

            reg.setRegx(regWord);
            reg.getMatcherResult(input);

            reg.setRegx(regTimes);
            reg.getMatcherResult(input);
        }

        public RegularExpression(){

        }

        public RegularExpression(String reg){
            this.regx = reg;
            this.pattern = Pattern.compile(regx);
        }


        public void getMatcherResult(String in){
            System.out.println("current regression expression is " + regx);
            this.matcher = pattern.matcher(in);

            while(matcher.find()){
                System.out.println(matcher.group());
            }
        }


        public void setRegx(String regx){
            this.regx = regx;
            this.pattern = Pattern.compile(regx);
        }
    }

参考文章

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值