正则表达式 In Java

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 $

matches return true if the whole string matches the given pattern. find tries to find a substring that matches the pattern.

    /**
     * 从dataStr中获取出所有的电话号码(固话和移动电话),将其放入Set
     * @param dataStr    待查找的字符串
     * @param 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);
    }



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值