难道这不应该将SPAN标记放在电话号码的外面,而不是里面吗?

String aParagraph = "start 201-555-1212 more (301)-777-1212 again (401) 888-1212 end";
String phoneRegEx = "\\b(\\(?[1-9]{1}[0-9]{2}\\)?[- ]?[1-9]{1}[0-9]{2}-[0-9]{4})\\b";
String replaceWith = "<span>$1</span>";

aParagraph = aParagraph.replaceAll(phoneRegEx , replaceWith);

最佳答案

单词边界\b\w+的边界匹配。您可以删除它们:

String aParagraph = "start 201-555-1212 more (301)-777-1212 again (401) 888-1212 end";
String phoneRegEx = "(\\(?[1-9]{1}[0-9]{2}\\)?[- ]?[1-9]{1}[0-9]{2}-[0-9]{4})";
String replaceWith = "<span>$1</span>";

aParagraph = aParagraph.replaceAll(phoneRegEx, replaceWith);

07-24 09:47