我有以下要求:正则表达式,用于提取电话号码
数(按顺序或连字符或空间隔开)
能有连字符 - [可选]
之间可以有空间之间[可选]
能有开括号(和闭括号) [可选]
能有加上开头+。 [可选]
现在,我使用(^ |)[0-9() - +] {5}(| $)
示例文本:
联系我们亚洲:(11)6530-6596-3242,9145465465465(011)4420717696024 +1 212-904-2860
我有以下要求:正则表达式,用于提取电话号码
数(按顺序或连字符或空间隔开)
能有连字符 - [可选]
之间可以有空间之间[可选]
能有开括号(和闭括号) [可选]
能有加上开头+。 [可选]
现在,我使用(^ |)[0-9() - +] {5}(| $)
示例文本:
联系我们亚洲:(11)6530-6596-3242,9145465465465(011)4420717696024 +1 212-904-2860
查找电话号码是一个相当困难的问题,因为不同的地区有不同的约定他们的电话号码秒。我建议libphonenumber,这是为了这个目的而在谷歌开发的一个图书馆。该库包含一个方法findNumbers(CharSequence, String)
,它可以查找电话号码并以解析方式返回它们。
String text = "Call me at +1 425 882-8080 for details.";
RegionCode country = RegionCode.US;
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
// Find the first phone number match:
PhoneNumberMatch m = util.findNumbers(text, country).iterator().next();
// rawString() contains the phone number as it appears in the text.
"+1 425 882-8080".equals(m.rawString());
// start() and end() define the range of the matched subsequence.
CharSequence subsequence = text.subSequence(m.start(), m.end());
"+1 425 882-8080".contentEquals(subsequence);
// number() returns the the same result as PhoneNumberUtil.parse()
// invoked on rawString().
util.parse(m.rawString(), country).equals(m.number());
这在任何情况下可能不会工作,但你应该有电话号码的多个输入规则,但你可以试试这个:[0-9\(\+]+[0-9\)\-]+[, ]?
后的一些例子。 – 2014-09-30 10:41:46
我可以推荐http://www.rubular.com/作为“工作台”来开发和测试你的正则表达式。还包含一个参考。 – Henrik 2014-09-30 10:42:23
@AvinashRaj发布。 – user3311019 2014-09-30 10:43:51