2014-09-30 65 views
-2

我有以下要求:正則表達式,用於提取電話號碼

數(按順序或連字符空間隔開)

能有連字符 - [可選]

之間

可以有空間之間[可選]

能有開括號(閉括號) [可選]

能有加上開頭+。 [可選]

現在,我使用(^ |)[0-9() - +] {5}(| $)

示例文本:

聯繫我們亞洲:(11)6530-6596-3242,9145465465465(011)4420717696024 +1 212-904-2860

+0

後的一些例子。 – 2014-09-30 10:41:46

+0

我可以推薦http://www.rubular.com/作爲「工作臺」來開發和測試你的正則表達式。還包含一個參考。 – Henrik 2014-09-30 10:42:23

+0

@AvinashRaj發佈。 – user3311019 2014-09-30 10:43:51

回答

0

查找電話號碼是一個相當困難的問題,因爲不同的地區有不同的約定他們的電話號碼秒。我建議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

這在任何情況下可能不會工作,但你應該有電話號碼的多個輸入規則,但你可以試試這個:[0-9\(\+]+[0-9\)\-]+[, ]?