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

标签 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;
}

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

最佳答案

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

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

关于android - 使用正则表达式从android中的网站文本数据中获取电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9954252/

相关文章:

java - 更改标题组和第一项菜单之间的抽屉间距

android - Android Firebase在ListView中检索数据错误

android - drawBitmap 其中一个像素等于一个像素

php - 正则表达式:如何指定捕获组应包含特定单词

javascript - 正则表达式仅接受正数和小数(仅以 .5 .0 或可接受的整数结尾)

java - Cookie 管理器导致致命信号 11

javascript - Javascript 中的基本正则表达式示例

powershell - 您必须在 '-' 运算符之后提供值表达式

c# - 如何使用表达式 linq 创建函数

c++ - 实现 A( :, k)=b; C++ 矩阵库中类似 Matlab 的语法:避免 const-ness