我创建了一个java电话簿(桌面应用程序),在我的电脑上有一个程序可以输出调用者的号码。这是一个 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 位或更多数字的正则表达式并使用它。您将能够从文本样本中提取这 2 个电话号码。
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]);
}
}
}
关于Java:抓取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329247/