我正在尝试从.txt文件中提取所有电话号码。该文件包含一些具有不同电话号码的文本,这些文本具有不同的长度和国家代码。文件中的一个例子是
not every line has a number
6/24/21, 12:14 am - +98 905 460 1134 joined using this group's invite link
6/23/21, 5:09 pm - +973 3345 9934 joined using this group's invite link
6/23/21, 5:09 pm - +968 9931 9003:
到目前为止,我所做的是
File file = new File("chat.txt");
Scanner content = new Scanner(file);
while(content.hasNext())
{
String line = content.nextLine();
String patternString = "(+[1-9]d{0,2}[- ]?)?[1-9]d{9}"; //pretty sure this is the issue
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(line);
boolean matchFound = matcher.find(); //should return true if it finds a phone number
if(matchFound){
String phone = ; //substring the found phone number
// output or write in a new file
}
}
缺少的是模式(regex(和电话号码的子字符串方式。不幸的是,我找不到这样做的解决方案。我们将非常感谢你的帮助。
public static void main(String[] args) throws Exception {
File file = new File("chat.txt");
String patternString = "\+\d{2,3}(?: \d{3,4}){2,3}";
Pattern pattern = Pattern.compile(patternString);
try (Scanner content = new Scanner(file)) {
while (content.hasNext()) {
String line = content.nextLine();
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
System.out.println(matcher.group());
}
}
}
}
是一种方法。非捕捉组,重复。
要查找字符串,请使用:
if(matcher.find()){
String phone = matcher.group(1);
// output or write in a new file
}
一个应该起作用的正则表达式示例是:
^+[1-9]{1}[0-9]{3,14}$