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);
}
精彩评论