C# 从字符串中只返回数字 0-9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/844461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:52:35  来源:igfitidea点击:

return only Digits 0-9 from a String

c#vb.netregexvbscriptcode-generation

提问by Brian Boatright

I need a regular expression that I can use in VBScript and .NET that will return only the numbers that are found in a string.

我需要一个可以在 VBScript 和 .NET 中使用的正则表达式,它只返回在字符串中找到的数字。

For Example any of the following "strings" should return only 1231231234

例如,以下任何“字符串”应仅返回1231231234

  • 123 123 1234
  • (123) 123-1234
  • 123-123-1234
  • (123)123-1234
  • 123.123.1234
  • 123 123 1234
  • 1 2 3 1 2 3 1 2 3 4
  • 123 123 1234
  • (123) 123-1234
  • 123-123-1234
  • (123)123-1234
  • 123.123.1234
  • 123 123 1234
  • 1 2 3 1 2 3 1 2 3 4

This will be used in an email parser to find telephone numbers that customers may provide in the email and do a database search.

这将用于电子邮件解析器以查找客户可能在电子邮件中提供的电话号码并进行数据库搜索。

I may have missed a similar regex but I did search on regexlib.com.

我可能错过了一个类似的正则表达式,但我确实在 regexlib.com 上进行了搜索。

[EDIT] - Added code generated by RegexBuddyafter setting up musicfreak's answer

[编辑] -在设置musicfreak的答案后添加了RegexBuddy生成的代码

VBScript Code

VBScript 代码

Dim myRegExp, ResultString
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "[^\d]"
ResultString = myRegExp.Replace(SubjectString, "")

VB.NET

网络

Dim ResultString As String
Try
      Dim RegexObj As New Regex("[^\d]")
      ResultString = RegexObj.Replace(SubjectString, "")
Catch ex As ArgumentException
      'Syntax error in the regular expression
End Try

C#

C#

string resultString = null;
try {
    Regex regexObj = new Regex(@"[^\d]");
    resultString = regexObj.Replace(subjectString, "");
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}

采纳答案by Sasha Chedygov

I don't know if VBScript has some kind of a "regular expression replace" function, but if it does, then you could do something like this pseudocode:

我不知道 VBScript 是否有某种“正则表达式替换”功能,但如果有,那么您可以执行以下伪代码:

reg_replace(/\D+/g, '', your_string)

I don't know VBScript so I can't give you the exact code but this would remove anything that is not a number.

我不知道 VBScript,所以我不能给你确切的代码,但这会删除任何不是数字的东西。

EDIT: Make sure to have the global flag (the "g" at the end of the regexp), otherwise it will only match the first non-number in your string.

编辑:确保有全局标志(正则表达式末尾的“g”),否则它只会匹配字符串中的第一个非数字。

回答by ólafur Waage

Have you gone through the phone nr categoryon regexlib. Seems like quite a few do what you need.

您是否在 regexlib 上浏览过电话号码类别。似乎有很多人做你需要的。

回答by Eoin Campbell

By the looks of things, your trying to catch any 10 digit phone number....

从表面上看,您试图抓住任何 10 位电话号码......

Why not do a string replace first of all on the text to remove any of the following characters.

为什么不首先对文本进行字符串替换以删除以下任何字符。

<SPACE> , . ( ) - [ ] 

Then afterwards, you can just do a regex search for a 10 digit number.

然后,您可以对 10 位数字进行正则表达式搜索。

\d{10}

回答by Matt Hamilton

In .NET, you could extract just the digits from the string. Like this:

在 .NET 中,您可以只从字符串中提取数字。像这样:

string justNumbers = new String(text.Where(Char.IsDigit).ToArray());

回答by richardtallent

Note: you've only solved half the problem here.

注意:这里你只解决了一半的问题。

For US phone numbers entered "in the wild", you may have:

对于“在野外”输入的美国电话号码,您可能有:

  • Phone numbers with or without the "1" prefix
  • Phone numbers with or without the area code
  • Phone numbers with extension numbers (if you blindly remove all non-digits, you'll miss the "x" or "Ext." or whatever also on the line).
  • Possibly, numbers encoded with mnemonic letters (800-BUY-THIS or whatever)
  • 带或不带“1”前缀的电话号码
  • 带或不带区号的电话号码
  • 带有分机号码的电话号码(如果您盲目地删除所有非数字,您将错过“x”或“Ext.”或其他任何内容)。
  • 可能是用助记字母编码的数字(800-BUY-THIS 或其他)

You'll need to add some smarts to your code to conform the resulting list of digits to a single standard that you actually search against in your database.

您需要在代码中添加一些智能,以使生成的数字列表符合您在数据库中实际搜索的单一标准。

Some simple things you could do to fix this:

你可以做一些简单的事情来解决这个问题:

  • Before the RegEx removal of non-digits, see if there's an "x" in the string. If there is, chop everything off after it (will handle most versions of writing an extension number).

  • For any number with 10+ digits beginning with a "1", chop off the 1. It's not part of the area code, US area codes start in the 2xx range.

  • For any number still exceeding 10 digits, assume the remainder is an extension of some sort, and chop it off.

  • Do your database search using an "ends-with" pattern search (SELECT * FROM mytable WHERE phonenumber LIKE 'blah%'). This will handle sitations (although with the possibility of error) where the area code is not provided, but your database has the number withthe area code.

  • 在 RegEx 去除非数字之前,查看字符串中是否有“x”。如果有的话,把它后面的所有东西都砍掉(将处理大多数版本的分机号码)。

  • 对于任何以“1”开头的 10+ 位数字,去掉 1。它不是区号的一部分,美国区号从 2xx 范围开始。

  • 对于仍然超过 10 位的任何数字,假设余数是某种扩展,并将其砍掉。

  • 使用“ends-with”模式搜索(SELECT * FROM mytable WHERE phonenumber LIKE 'blah%')进行数据库搜索。这将处理未提供区号但您的数据库具有区号的号码的情况(尽管有可能出错)。

回答by richardtallent

In respect to the points made by richardtallent, this code will handle most of your issues in respect to extension numbers, and the US country code (+1) being prepended.

关于richardtallent 提出的观点,此代码将处理您与分机号码相关的大部分问题,并在前面添加美国国家/地区代码(+1)。

Not the most elegant solution, but I had to quickly solve the problem so I could move on with what I'm doing.

不是最优雅的解决方案,但我必须快速解决问题,以便我可以继续我正在做的事情。

I hope it helps someone.

我希望它可以帮助某人。

 Public Shared Function JustNumbers(inputString As String) As String
        Dim outString As String = ""
        Dim nEnds As Integer = -1

        ' Cycle through and test the ASCII character code of each character in the string. Remove everything non-numeric except "x" (in the event an extension is in the string as follows):
        '    331-123-3451 extension 405  becomes 3311233451x405
        '    226-123-4567 ext 405        becomes 2261234567x405
        '    226-123-4567 x 405          becomes 2261234567x405
        For l = 1 To inputString.Length
            Dim tmp As String = Mid(inputString, l, 1)
            If (Asc(tmp) >= 48 And Asc(tmp) <= 57) Then
                outString &= tmp
            ElseIf Asc(tmp.ToLower) = 120
                outString &= tmp
                nEnds = l
            End If
        Next


        ' Remove the leading US country code 1 after doing some validation
        If outString.Length > 0 Then
            If Strings.Left(outString, 1) = "1" Then

                ' If the nEnds flag is still -1, that means no extension was added above, set it to the full length of the string
                ' otherwise, an extension number was detected, and that should be the nEnds (number ends) position.
                If nEnds = -1 Then nEnds = outString.Length

                ' We hit a 10+ digit phone number, this means an area code is prefixed; 
                ' Remove the trailing 1 in case someone put in the US country code
                ' This is technically safe, since there are no US area codes that start with a 1. The start digits are 2-9
                If nEnds > 10 Then
                    outString = Right(outString, outString.Length - 1)
                End If
            End If
        End If

        Debug.Print(inputString + "          : became : " + outString)

        Return outString
    End Function

回答by Teodor Tite

As an alternative to the main .Netsolution, adapted from a similar question'sanswer:

作为主要.Net解决方案的替代方案,改编自类似问题的答案:

string justNumbers = string.Concat(text.Where(char.IsDigit));

回答by Nur.B

The simplest solution, without a regular expression:

最简单的解决方案,没有正则表达式:

public string DigitsOnly(string s)
   {
     string res = "";
     for (int i = 0; i < s.Length; i++)
     {
       if (Char.IsDigit(s[i]))
        res += s[i];
     }
     return res;
   }