1

I have to save text data of a website to the memory card. Afterwards, I'd like to use regular expressions to fetch a phone number.

I use this code to fetch the website:

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

However I can't fetch the phone number from text data. Could you help me please?

Nguyen Nguyen
  • 47
  • 2
  • 9
  • Which phone number would you like to extract? I didn't find any on http://vnexpress.net/ – SecStone Mar 31 '12 at 09:17
  • You have to provide the text from which you wish to extract the phone number, otherwise, no one will know what pattern to match what number. – neevek Mar 31 '12 at 09:21
  • vnexpress.net is one example .So ,Please tell me How to use regular expressions to fetch a number phone from a text data of web ?. – Nguyen Nguyen Mar 31 '12 at 09:35

1 Answers1

3

you can try this site for the regex

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
Win Myo Htet
  • 5,377
  • 3
  • 38
  • 56