【问题标题】:Use regular expressions process string in Android在Android中使用正则表达式处理字符串
【发布时间】:2012-12-20 07:58:57
【问题描述】:

我通过 HttpGet 获得响应。在 getEntity().getContent() 之后,我得到了 HTML 页面的代码,然后将此页面转换为 String pageHTML。

我需要用正则表达式匹配pageHTML,然后得到结果。

我已经创建了正则表达式。

如果正则表达式只是返回一个值,如何创建? 如果正则表达式只返回n个值,如何创建?

【问题讨论】:

    标签: java android regex expression


    【解决方案1】:

    您可以使用组从正则表达式接收多个值。详情请见this

    Pattern datePatt = Pattern.compile("([0-9]{2})/([0-9]{2})/([0-9]{4})");
    
    Matcher m = datePatt.matcher(dateStr);
    if (m.matches()) {
      int day   = Integer.parseInt(m.group(1)); // get values inside the first (..)
      int month = Integer.parseInt(m.group(2)); // get values inside the second (..)
      int year  = Integer.parseInt(m.group(3)); // get values inside the third (..)
    }
    

    【讨论】:

      【解决方案2】:

      使用Pattern 创建您的正则表达式。然后您可以致电pattern. matcher(pageHTML) 获取Matcher

      Matcher可以让你知道有没有matchesfind有没有下一个匹配,取group代表最后一个匹配的子序列。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        相关资源
        最近更新 更多