我有这样的字符串
<li class="number">206-861-1109</li>206-861-1365</li>er">206-861-4532</l
我要提取此模式中的所有数字-206-861-****
所以输出应该是
206-861-1109
206-861-1365
206-861-4532
我该怎么办?通过正则表达式可以吗?如果是,那怎么办?
答案 0 :(得分:2)
使用正则表达式206-861-\d{4}
:
Pattern pattern = Pattern.compile("206-861-\\d{4}");
Matcher matcher = pattern.matcher("<li class=\"number\">206-861-1109</li>206-861-1365</li>er\">206-861-4532</l");
while (matcher.find()) {
System.out.println(matcher.group());
}
输出:
206-861-1109
206-861-1365
206-861-4532
答案 1 :(得分:1)
可能如下:
String str = "<li class=\"number\">206-861-1109</li>206-861-1365</li>er\">206-861-4532</l";
// or \\d could be used instead of [0-9]
Pattern pattern = Pattern.compile("206-861-[0-9]*");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.print(matcher.group() + " ");
}