我创建了一个Java电话簿(桌面应用程序),在我的PC上有一个程序可以输出呼叫者的电话号码。这是一个8位数字。

这是如何运作的

我只想从弹出窗口中抓取8位数字,所以可以说这是一个弹出窗口:

    My name is someone like you, i am 22 years old, i was born in 19/10/1989,
    my phone number is 34544512
    my brother is someone like me he is 18 years old, born in 9101993
    his number is 07777666


在这个例子中,我只想抓取07777666和34544512。

我想每2秒检查一下弹出窗口中是否有新号码,如果呼叫者给我打电话了两次,他的号码将已经存储在我的数据库中;否则,我将存储

注意:如果无法做到这一点,那就不用担心弹出窗口了,可以说这只是每2秒更新一次的文本,以及如何抓取它

这不是家庭作业大声笑:D

最佳答案

使用Java正则表达式。创建8个或更多数字的正则表达式并使用它。您将能够从文本样本中提取这两个电话号码。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String args[]) throws Exception {
        String testString = "My name is someone like you, i am 22 years old, i was born in 19/10/1989,"
                          + " my phone number is  34544512 3454451266"
                          + " my brother is someone like me he is 18 years old, born in 9101993 "
                          + " his number is 07777666";

        String[] pieces = testString.split("\\s+");
        String expression = "\\d{8,}";
        Pattern pattern = Pattern.compile(expression);
        for (int i = 0; i < pieces.length; i++) {
            if (pattern.matches(expression, pieces[i]))
                System.out.println(pieces[i]);
        }
    }
}

10-07 19:21
查看更多