JavaSE-正则表达式

使用正则表达式处理字符串

使用到的类:java.util.regex包中的Pattern和Matcher类

Pattern类没有构造函数,只提供了静态方法compile(),用来接收文本形式的正则表达式,然后编译成Pattern类的对象

简单使用

public class Demo1 {
    public static void main(String[] args) {
        //Pattern表示编译后的正则表达式
        Pattern pattern = Pattern.compile("正则表达式");
        //pattern检查接收到的目标文本
        Matcher matcher = pattern.matcher("正则表达式");
        //进行匹配
        boolean myBoolean = matcher.matches();
        System.out.println(myBoolean);
    }
}

输出:true

Pattern常用方法

Pattern pattern = Pattern.compile("wozhenshuai");
        String s = pattern.pattern();
        System.out.println(s);
        //String pattern() 该方法用于返回正则表达式的字符串形式

输出:wozhenshuai
Pattern pattern = Pattern.compile(":");

String []spilt = pattern.split("One:two:three",3);

//根据模式和限制拆分指定输入序列
//将:作为分割,limit表示分成几部分
for(String elem:spilt){
   System.out.println(elem);
}

String []split(charSequence input,in limit) 用于根据模式和限制拆分指定的输入序列
输出:One
     two 
     three
boolean matches = Pattern.matches("is","is");
System.out.println(matches);

static boolean matches (String regex,CharSequence input)
用于编译指定正则表达式并尝试根据它来匹配指定输入


输出:true

 Matcher的常用方法

        Pattern pattern = Pattern.compile("is");

        Matcher matcher = pattern.matcher("This is a text");

        while(matcher.find()){
            //boolean find() 尝试在文本中查找与模式匹配的下一个子序列
            System.out.println(matcher.start());
            //int start() 返回上一个匹配的开始的索引
            System.out.println(matcher.end());
            //返回上一个匹配完成后的偏移量,因为is的长度是2所以偏移2个长度

        }

输出:
Match founded
2
4
Match founded
5
7



        Pattern pattern = Pattern.compile("This");
        Matcher matcher = pattern.matcher("This is a text");
        System.out.println(matcher.replaceAll("That"));
        //String replaceAll(String replacement) 替换字符串的每个子序列
输出
That is a text
        Pattern pattern = Pattern.compile("is");
        Matcher matcher = pattern.matcher("This is a text");
        System.out.println(matcher.replaceFirst("are"));
        //String replaceFirst(String replacement) 替换字符串的第一个子序列

输出:
Thare is a text
        Pattern pattern = Pattern.compile("and");
        Matcher matcher = pattern.matcher("He is good and He is handsome and aaa");
        StringBuffer s = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(s,"John");
            System.out.println(s);
            //将John替换and
            //输出第一个John之前的文本
            //输出第二个John之前的文本
            //...
            //输出第n个John之前的文本

输出:
He is good John
He is good John He is hJohn
He is good John He is hJohnsome John

 使用字符类

[def]
[^def]
[a-zA-Z]
[b-e[n-q]]
[a-z&&[abc]]
[a-z&&[^bcd]] 即[ae-z]
[a-z&&[^n-p]] 即[a-mq-z]

使用计数器

确定一个或一串字符在表达式中出现的次数

使用限定符

Greedy:贪婪限定符,模式从最大重复次数开始尝试匹配,匹配不上不断递减重复次数并尝试

Reluctant:懒惰限定符,模式从最小重复次数开始尝试匹配,匹配不上不断递增重复次数并尝试

 Possessive:独占限定符,模式仅按照最大的重复次数匹配

        String text = "When in Rome, do as the Romans";
        String textSplit[] = text.split(" ");
        System.out.println(textSplit.length);
        //根据空格为划分,将数组划分成了7份,textSpilt[0] = "When"
        Pattern pattern = Pattern.compile("Ro.+");
        for(int i = 0;i<textSplit.length;i++){
            Matcher matcher = pattern.matcher(textSplit[i]);
            //字串与每个单词比较,如果开头是Ro返回true
            //XX.+表示以XX开头    .+XX表示以XX结尾
            System.out.println(matcher.matches());
        }

输出:
7
false
false
true
false
false
false
true

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值