使用正则表达式捕获在不同点插入空格的电话号码

时间:2017-03-05 22:25:18

标签: regex

我希望能够从文本中提取完整的电话号码,而不管有多少空格中断号码。

例如在文章中:

I think Emily was her name, and that her number was either 0421032614 or 0423 032 615 or 04321 98 564

我想提取:

0421032614
0423032615
0432198564

我可以使用

提取前两个
(\d{4}[\s]?)(\d{3}[\s]?)+

但这取决于我提前知道十个数字将如何分组(即空格将在何处)。有没有办法用更灵活的模式捕获十个数字?

1 个答案:

答案 0 :(得分:0)

您需要删除所有空格,然后运行for循环并遍历组:

    public static void main (String [] args){
    String reg = "(\\d{10})";
    String word = " think Emily was her name, and that her number was either 0421032614 or 0423 032 615 or 04321 98 564";

    word = word.replaceAll("\\s+",""); // replace all the whitespace with nothing

    Pattern pat = Pattern.compile(reg);
    Matcher mat = pat.matcher(word);

    while (mat.find()) {
        for (int i = 1; i <= mat.groupCount(); i++) {
             System.out.println(mat.group(i));
        }
    } 

}

输出

0421032614
0423032615
0432198564