【问题标题】:get list of string with matcher in java/android在 java/android 中获取带有匹配器的字符串列表
【发布时间】:2015-06-23 20:23:47
【问题描述】:

我有一个类似

的字符串
number +919999999990  time at:07:42:45 on 10.04.2014, number
+919999999991 time at:08:42:45 on 11.05.2014,  number +919999999992 time at:075:42:45 on 05.03.2014 , number +9199999999913 ,time at:27:40:45 on 09.04.2015.

我必须检索所有电话号码、日期和时间。 我打算使用模式和匹配器。

我的代码是

String extra1="number +919999999990  time at:07:42:45 on 10.04.2014, number +919999999991 time at:08:42:45 on 11.05.2014, number +919999999992 time at:075:42:45 on 05.03.2014 , number +9199999999913 ,time at:27:40:45 on 09.04.2015.";
Pattern pattern = Pattern.compile("\\+\\d{12}");
Matcher matcher = pattern.matcher(extra1);
System.out.println("matcher.groupCount() "+matcher.groupCount());
if (matcher.find()) {
    while(matcher.find()) {
        System.out.println(matcher.group());
    }   
} else {
     System.out.println("Not Found");
}

根据thisthis 它应该打印所有电话号码。 但我只得到第一个电话号码。

任何人都可以提出解决方案...

【问题讨论】:

  • 在控制台应用程序中,打印除第一个数字之外的所有数字。 (它不会打印第一个数字,因为您在打印任何内容之前调用了两次 find...)
  • @Jon Skeet 谢谢...

标签: java android string list matcher


【解决方案1】:

如果您删除 'if (matcher...)' 语句,它将消耗第一个数字,它对我来说很好:

    String extra1 = "number +919999999990  time at:07:42:45 on 10.04.2014, number +919999999991 time at:08:42:45 on 11.05.2014, number +919999999992 time at:075:42:45 on 05.03.2014 , number +9199999999913 ,time at:27:40:45 on 09.04.2015.";
    Pattern pattern = Pattern.compile("\\+\\d{12}");
    Matcher matcher = pattern.matcher(extra1);
    System.out.println("matcher.groupCount() " + matcher.groupCount());
    while (matcher.find()) {
        System.out.println(matcher.group());
    }

输出:

matcher.groupCount() 0
+919999999990
+919999999991
+919999999992
+919999999991

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    相关资源
    最近更新 更多