java - 在java/android中获取带有匹配器的字符串列表

标签 java android string list matcher

我有一个像

这样的字符串
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它应该打印所有电话号码。 但我只得到第一个电话号码。

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

最佳答案

如果您删除将消耗第一个数字的“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

关于java - 在java/android中获取带有匹配器的字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691494/

相关文章:

java - 如何在Eclipse中调试openjdk 9(主要是hotspot)源码?

java - RXJava 忽略错误并在链中继续

string - 访问Lua的内置字符串哈希函数

c# - 如何根据时区解析日期和时间?

java - 如何对二维数组进行升序排序? java

java - 如何在 javacv 中访问 CvSeq 中的一个点?

java - serialversionuid 是静态的和最终的,但为什么它是可序列化的

java - 使用Eclipse,layout.xml文件上的TabLayout错误

android - 我的属性始终是MVVM模型中的默认值

c++ - 优化静态字符串的字符串存储