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

正则表达式 In Java

发布时间:2020-12-14 00:39:32 所属栏目:百科 来源:网络整理
导读:java里没有类似python的search 函数 怎样得到matched的字符串? group find/matched判断是否matched. Difference between matches() and find() in Java Regex matches tries to match the expression against the entire string and implicitly add a ^ at

java里没有类似python的search 函数

怎样得到matched的字符串? group

find/matched判断是否matched.

Difference between matches() and find() in Java Regex

matches tries to match the expression against the entire string and implicitly add a ^ at the start and $ at the end of your pattern,meaning it will not look for a substring.
matches隐含在正则表达式前后增加了: ^ and $

matchesreturn true if the whole string matches the given pattern.findtries to find a substring that matches the pattern.

    /**
     * 从dataStr中获取出所有的电话号码(固话和移动电话),将其放入Set
     * @param dataStr    待查找的字符串
     *  phoneSet    dataStr中的电话号码
     */
    public static void getPhoneNumFromStrIntoSet(String dataStr,Set<String> phoneSet)
    {
        //获得固定电话和移动电话的正则表达式
        String regexp = isPhoneRegexp();
        
        System.out.println("Regexp = " + regexp);
        
        Pattern pattern = Pattern.compile(regexp); 
        Matcher matcher = pattern.matcher(dataStr); 

        找与该模式匹配的输入序列的下一个子序列
        while (matcher.find()) 
        { 
            获取到之前查找到的字符串,并将其添加入set中
            phoneSet.add(matcher.group());
        } 
        System.out.println(phoneSet);
    }

(编辑:李大同)

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

    推荐文章
      热点阅读