代码之家  ›  专栏  ›  技术社区  ›  Brian Boatright

仅返回字符串中的数字0-9

  •  78
  • Brian Boatright  · 技术社区  · 17 年前

    1231231234

    • 123 123 1234

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

    RegexBuddy

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

    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

    string resultString = null;
    try {
        Regex regexObj = new Regex(@"[^\d]");
        resultString = regexObj.Replace(subjectString, "");
    } catch (ArgumentException ex) {
        // Syntax error in the regular expression
    }
    
    8 回复  |  直到 17 年前
        1
  •  196
  •   Richard Garside    9 年前

    在。NET,你可以只从字符串中提取数字。使用Linq如下:

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

    using System.Linq

        2
  •  14
  •   Sasha Chedygov    17 年前

    .Net 解决方案,改编自 similar question's 答案:

    string justNumbers = string.Concat(text.Where(char.IsDigit));
    
        3
  •  14
  •   Community Mohan Dere    9 年前

    我不知道VBScript是否有某种“正则表达式替换”函数,但如果有,那么你可以做这样的伪代码:

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

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

        4
  •  7
  •   richardtallent    17 年前

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

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

    • 带或不带区号的电话号码
    • 可能是用助记符字母编码的数字(800-BUY-THIS或其他)

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

    • 对于任何仍超过10位的数字,假设余数是某种形式的扩展,并将其截断。

    • 使用“以结尾”模式搜索进行数据库搜索(从mytable的WHERE电话号码LIKE'blah%'中选择*FROM)。这将处理没有提供区号但您的数据库有号码的情况(尽管有可能出错) 随着

        5
  •  1
  •   Eoin Campbell    17 年前

    从表面上看,你试图抓住任何10位数的电话号码。...

    为什么不先用字符串替换文本,以删除以下任何字符。

    <SPACE> , . ( ) - [ ] 
    

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

    \d{10}
    
        6
  •  0
  •   Ólafur Waage    17 年前

    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;
       }
    
        7
  •  0
  •   user4914655 user4914655    10 年前
        8
  •  0
  •   Nur.B    6 年前

    这不是最优雅的解决方案,但我必须快速解决问题,这样我才能继续做我正在做的事情。

    我希望它能帮助别人。

     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