我发现的其他答案不适用于我拥有的字符串。我的字符串看起来像这样:
Tel.: 123 12 12 31 12\nMobil: 0173 84 44 962\nE-Mail: [email protected]
我已经足够获得出现的第一个电话号码。文本是自由文本,因此人们实际上可以输入他们想要的任何内容,甚至根本没有数字。
我试过了
Pattern pattern = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
Matcher matcher = pattern.matcher(contactText);
if (matcher.find()) {
phoneLabel.setText(matcher.group(0));
}
这个正则表达式是德国数字
^(((((((00|\+)49[ \-/]?)|0)[1-9][0-9]{1,4})[ \-/]?)|((((00|\+)49\()|\(0)[1-9][0-9]{1,4}\)[ \-/]?))[0-9]{1,7}([ \-/]?[0-9]{1,5})?)$
要匹配示例中的电话号码,请使用:
(?:\d+\s*)+
try {
Pattern regex = Pattern.compile("(?:\\d+\\s*)+");
Matcher regexMatcher = regex.matcher(contactText);
if (regexMatcher.find()) {
phoneLabel.setText(regexMatcher.group(0));
}
} catch (PatternSyntaxException ex) {
// Syntax error
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句