开发者

Retrieve numbers separated by '-'

Lets say I have a large amount of (random) text. Within this text there is a phone number, consisting of three digits, a dash, another three digits, a dash, and four digits. For example, XXX-XXX-XXXX. What would be the regex for retrieving this number from the text. I tried using:

Matcher matcher = pattern.matcher(previousText);
Pattern pattern2 = Pattern.compile(".*(\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d).*")
Matcher matcher2 = pattern2.matcher(currentText);
开发者_C百科

Now, I though it would work, but it doesn't. Please help.


The regex: \d{3}-\d{3}-\d{4}


Pattern pattern = Pattern.compile(".*(\\d{3}-\\d{3}-\\d{4}).*");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
    String number = matcher.group(1);
    System.out.println(number);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜