我正在尝试使用正则表达式在杂乱的html文档中查找形式为(xxx) xxx-xxxx的电话号码。
文本文件中的行类似于:
<div style="font-weight:bold;">
<div>
<strong>Main Phone:
<span style="font-weight:normal;">(713) 555-9539
<strong>Main Fax:
<span style="font-weight:normal;">(713) 555-9541
<strong>Toll Free:
<span style="font-weight:normal;">(888) 555-9539
我的代码包含:
Pattern p = Pattern.compile("\\(\\d{3}\\)\\s\\d{3}-\\d{4}");
Matcher m = p.matcher(line); //from buffered reader, reading 1 line at a time
if (m.matches()) {
stringArray.add(line);
}
问题在于,即使我将简单的内容放入模式中编译,它仍然返回空。如果它甚至不能识别像\d这样的内容,那么我怎么能获取电话号码呢?例如:
Pattern p = Pattern.compile("\\d+"); //Returns nothing
Pattern p = Pattern.compile("\\d"); //Returns nothing
Pattern p = Pattern.compile("\\s+"); //Returns lines
Pattern p = Pattern.compile("\\D"); //Returns lines
对我来说这真的很困惑,任何帮助都将不胜感激。