使用正则表达式从android中的网站文本数据中获取数字电话

时间:2012-03-31 08:16:02

标签: android regex expression

我必须将网站的文本数据保存到存储卡。之后,我想使用正则表达式来获取电话号码。

我使用此代码来获取网站:

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.save);
    TextView saveText = (TextView) findViewById(R.id.save_text);
    String t = "";
    String urltext = "http://vnexpress.net/";
    try {
        t = get_source_html(urltext);
    } catch (Exception e) {
        e.toString();
    }
    saveText.setText(t);
}

public static String get_source_html(String urltext) throws IOException {
    URL url = new URL(urltext);
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String inputLine;
    String s = "";
    while ((inputLine = in.readLine()) != null) {
        s = s + inputLine;
    }
    s = s + "";
    in.close();
    return s;
}

但我无法从文本数据中获取电话号码。你能帮帮我吗?

1 个答案:

答案 0 :(得分:3)

你可以试试这个网站的正则表达式

http://regexlib.com/Search.aspx?k=phone&AspxAutoDetectCookieSupport=1

Title   Test
Details
 International & Domestic Phone Numbers with Ext 
Expression  ^([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)$
Description Somewhat conservative expression for evaluating phone numbers. Based off ideas found at http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation this allows country codes, with or without the + symbol, area codes surrounded by parenthesis (or not) and numbers delimited by spaces, periods or dashes. You'll want to independently test for string length based on your needs.
Matches (123)456-7890 | (123)456-7890 x123 | +1 (123)456-7890 | 12 3456 789 0 x1234 | (123)456-7890x123 |(123)456-7890ext123 | (123)456-7890 extension123 | 123.456.7890 | 1234567890 | 1234567 | 12 34 56 78 90 | 12 3 4567 890123 x4567 | +12 3456 7890 | +12 34 56 7890 | +12 3456 7890 | +12 34567890
Non-Matches (123)456-7890 x 123 | 123.45.6.78+90