如何获取带有加号和括号的电话号码并仅返回号码?

发布于 2024-07-17 01:20:07 字数 65 浏览 6 评论 0原文

我有一个简单的问题,我有一个这样的电话:+1 (123) 123-1234,我想使用正则表达式从该字符串中取出数字。

I have a simple problem I have a phone like this: +1 (123) 123-1234 and I want to just take the numbers out of that string using regex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌逼全场 2024-07-24 01:20:08

这将删除非数字字符:

string input = "+1 (123) 123-1234";
string digits = Regex.Replace(input,@"\D",string.Empty);

This will strip out an non-numeric characters:

string input = "+1 (123) 123-1234";
string digits = Regex.Replace(input,@"\D",string.Empty);
飞烟轻若梦 2024-07-24 01:20:08

使用正则表达式是一种解决方案。 另一种方法是使用 LINQ(假设您使用的是 .Net 3.5)

    string myPhone = "+1 (123) 123-1234";
    string StrippedPhone = new string((from c in myPhone
                                       where Char.IsDigit(c)
                                       select c).ToArray());

最终结果是相同的,但我认为在这种情况下 LINQ 比 RegEx 提供了一些优势。 首先,可读性。 正则表达式要求您知道“D”表示非数字(与 Char.IsDigit() 相比) - 此处的注释中已经存在对此的混淆。 另外,我做了一个非常简单的基准测试,每种方法执行 100,000 次。

LINQ:127ms

RegEx:485ms

因此,乍一看,在这种情况下 LINQ 的性能似乎优于 Regex。 而且,我认为它更具可读性。

    int i;
    int TIMES = 100000;
    Stopwatch sw = new Stopwatch();
    string myPhone = "+1 (123) 123-1234";

    // Using LINQ            
    sw.Start();
    for (i = 0; i < TIMES; i++)
    {
        string StrippedPhone = new string((from c in myPhone
                                           where Char.IsDigit(c)
                                           select c).ToArray());
    }
    sw.Stop();
    Console.WriteLine("Linq took {0}ms", sw.ElapsedMilliseconds);

    // Reset 
    sw.Reset();

    // Using RegEx
    sw.Start();
    for (i = 0; i < TIMES; i++)
    {
        string digits = Regex.Replace(myPhone, @"\D", string.Empty);
    }
    sw.Stop();
    Console.WriteLine("RegEx took {0}ms", sw.ElapsedMilliseconds);

    Console.ReadLine();

Using RegEx is one solution. Another way would be to use LINQ (provided you are using .Net 3.5)

    string myPhone = "+1 (123) 123-1234";
    string StrippedPhone = new string((from c in myPhone
                                       where Char.IsDigit(c)
                                       select c).ToArray());

The end result is the same, but I think LINQ offers some advantages over RegEx in this case. First, readability. The RegEx requires you to know that "D" means Non digit (compared to Char.IsDigit())- there is confusion about that already in the comments here. Also, I did a very simple benchmark, performing each method 100,000 times.

LINQ: 127ms

RegEx: 485ms

So, at a quick glance, it seems like LINQ out performs Regex in this situation. And, I'd argue it is more readable.

    int i;
    int TIMES = 100000;
    Stopwatch sw = new Stopwatch();
    string myPhone = "+1 (123) 123-1234";

    // Using LINQ            
    sw.Start();
    for (i = 0; i < TIMES; i++)
    {
        string StrippedPhone = new string((from c in myPhone
                                           where Char.IsDigit(c)
                                           select c).ToArray());
    }
    sw.Stop();
    Console.WriteLine("Linq took {0}ms", sw.ElapsedMilliseconds);

    // Reset 
    sw.Reset();

    // Using RegEx
    sw.Start();
    for (i = 0; i < TIMES; i++)
    {
        string digits = Regex.Replace(myPhone, @"\D", string.Empty);
    }
    sw.Stop();
    Console.WriteLine("RegEx took {0}ms", sw.ElapsedMilliseconds);

    Console.ReadLine();
遮云壑 2024-07-24 01:20:08
string digits = Regex.Replace(input, @"[^\d]", String.Empty);
string digits = Regex.Replace(input, @"[^\d]", String.Empty);
梓梦 2024-07-24 01:20:08

为什么不直接更换呢?

string phoneNumber = "+1 (123) 123-1234";
phoneNumber = phoneNumber.Replace("+", "");
phoneNumber = phoneNumber.Replace("(", "");
phoneNumber = phoneNumber.Replace(")", "");
phoneNumber = phoneNumber.Replace("-", "");
phoneNumber = phoneNumber.Replace(" ", "");

Why not just do a replace?

string phoneNumber = "+1 (123) 123-1234";
phoneNumber = phoneNumber.Replace("+", "");
phoneNumber = phoneNumber.Replace("(", "");
phoneNumber = phoneNumber.Replace(")", "");
phoneNumber = phoneNumber.Replace("-", "");
phoneNumber = phoneNumber.Replace(" ", "");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文