-2

I want to build a method that will get a string (preferably the text of a textblock) and it will identify and highlight any phone numbers in the string. The goal is to enable the user to tap any number and directly call or text it(by using the appropriate launcher).

How can I work this out? Any ideas? Thank you in advance!

Icarus
  • 139
  • 2
  • 13
  • Do you want a solution for extracting phone numbers or enabling the user to tap a number and call/text? – James Jeffery Aug 18 '13 at 08:49
  • http://stackoverflow.com/questions/1559753/regular-expression-to-match-us-phone-numbers Using a Regex like this may help you. – Dave Bish Aug 18 '13 at 08:49
  • http://stackoverflow.com/questions/3764814/how-to-detect-telephone-numbers-in-a-text-and-replace-them Possible repeated. – jbaylina Aug 18 '13 at 08:49
  • For extracting phone numbers, it's already been answered: http://stackoverflow.com/questions/6175691/regex-number-phone – James Jeffery Aug 18 '13 at 08:49
  • Thank you all for your responses, they are all great but I prefer to keep it simple, that is avoid using regular expressions. – Icarus Aug 18 '13 at 09:33

2 Answers2

2

You can use Regular expression to do this.

Example:-

var s= new Regex(@"(\(?[0-9]{3}\)?)?\-?[0-9]{3}\-?[0-9]{4}",  
RegexOptions.IgnoreCase); //North American number

var text = "Some Texxt";
MatchCollection m= s.Matches(text);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1
String s = "abc055667788abc";
string phoneNumber;
foreach(char c in s)
{
    if(Char.isNumber(c) || c == " " || c == "+")
    {
        phoneNumber = phoneNumber + c;
        minimumDigits++;
        if(minimumDigits >= 9)
        {
            NumberDetected(phoneNumber);
        }
    }
    else
    {
        minimumDigits = 0;
    }
}

NumberDetected(string rawNumber)
{ 
    int plusses = 0;
    foreach(char c in rawNumber)
    {
        if(c == "+")
        {
            plusses++;
        }
    }
    if(plusses <= 1)
    {
        if(rawNumber.StartsWith("+")
        {
            NumberDone(rawNumber);
        }
    }
    else
    {
        MessageBox.Show("Number contained too many plusses!");
    }
}
Patrick Geyer
  • 1,515
  • 13
  • 30
  • This seems to be the simplest approach, thanks! – Icarus Aug 18 '13 at 09:30
  • 1
    @Icarus no problem. Do let us know how you solved the click to call problem :) – Patrick Geyer Aug 18 '13 at 09:50
  • This would match `1+2+3+4+5` too.... – I4V Aug 18 '13 at 10:03
  • @I4V there is a effort/result factor involved. Why don't you provide a better answer that never misses a phone number at all. But if you want another layer of stability, let me know and I'll put some code in the NumberDetected method. – Patrick Geyer Aug 18 '13 at 10:24
  • Now it will match `1234+5678` – I4V Aug 18 '13 at 10:29
  • @I4V may you remove your downvote for my effort, please? In fact, an upvote would benefit me greatly! – Patrick Geyer Aug 18 '13 at 10:38
  • @PatrickGeyer method's name is `IsNumber` not `isNumber`. `1 2 3 4 5` is not a phone number etc. I just removed my downvote for your effort. But my personal opinion, it is completely useless... – I4V Aug 18 '13 at 10:40
  • @I4V I understand what your saying, but I just gave an example for Icarus so that he gets going in the right direction. He will have to write his code himself. Everyone has their own opinion, but I wouldn't call my post completely useless - especially after it's been marked as an answer. – Patrick Geyer Aug 18 '13 at 10:44
  • All I wanted when I asked this question was an idea (or some ideas) to get started. I will write my own code that will serve my app's purpose of course. I did not expect a tailor-made answer. In this context, Patrick's answer was what I was looking for. As for the click-to-call issue, I am just thinking to enable a secondary menu that will appear if the textblock is tapped and a phone number is detected in its text. – Icarus Aug 18 '13 at 17:59
  • @Icarus good luck on your project! – Patrick Geyer Aug 18 '13 at 18:01