加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

获取功能- 模式Pattren 和匹配器 Matcher

发布时间:2020-12-14 00:44:02 所属栏目:百科 来源:网络整理
导读:正则表达式基础知识 获取 模式Pattren 和匹配器 Matcher 1.获取模式Pattern Pattern pattern=Pattern.compile("a*b*"); 2.获取匹配器Matcher Matcher matcher=pattern.matcher("aaa"); 模式Pattren 和匹配器 Matcher的获取 public static void main(String[]
正则表达式基础知识


获取 模式Pattren 和匹配器 Matcher

1.获取模式Pattern
Pattern pattern=Pattern.compile("a*b*");
2.获取匹配器Matcher

Matcher matcher=pattern.matcher("aaa");


模式Pattren 和匹配器 Matcher的获取

public static void main(String[] args){
  //获取模式Pattern
  Pattern pattern=Pattern.compile("a*b*");
  //获取匹配器Matcher
  Matcher matcher=pattern.matcher("aaa");


  boolean b=matcher.matches();
  System.out.print(b);


获取字符串中长度为3的单词

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

/**
* Created by hanshan on 2016/12/29.
*/
public class Matcher和Pattern2 {
    public static void main(String[] args){
        String s="wo ai wo de jia,tom loves his jia too";
        Pattern pattern=Pattern.compile("bw{3}b");
        Matcher matcher=pattern.matcher(s);
    /*    boolean b=matcher.find();
        System.out.print(b);
        String result=matcher.group();
        System.out.print(result);*/
    while(matcher.find()){
        String ans=matcher.group();
        System.out.print(ans+"  ");
    }
        // 注意:一定要先find(),然后才能group()
        // IllegalStateException: No match found
        // String ss = m.group();
        // System.out.println(ss);
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读