只返回string中的数字0-9

我需要一个正则expression式,我可以在VBScript和.NET中使用,只返回在string中find的数字。

例如下面的任何“string”应该只返回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

这将在电子邮件parsing器中用于查找客户可能在电子邮件中提供的电话号码并执行数据库search。

我可能错过了一个类似的正则expression式,但我在regexlib.com上search。

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

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#

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

我不知道VBScript是否具有某种“正则expression式replace”function,但如果是这样的话,那么你可以做这样的伪代码:

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

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

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

在.NET中,你可以只提取string中的数字。 喜欢这个:

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

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

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

  • 带或不带“1”前缀的电话号码
  • 有或没有区号的电话号码
  • 带有分机号码的电话号码(如果您盲目地删除所有非数字,您将错过“x”或“Ext。”或其他任何行)。
  • 可能用数字编码(800-BUY-THIS或其他)

您需要添加一些智能到您的代码,以使得到的数字列表符合您在数据库中实际search到的单个标准。

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

  • 在RegEx删除非数字之前,查看string中是否有“x”。 如果有的话,把所有东西都砍掉(处理大部分版本的分机号码)。

  • 对于任何以“1”开始的10位以上的数字,切断1.这不是区号的一部分,US区号从2xx开始。

  • 对于任何数字仍然超过10位,假设其余的是某种types的延伸,并砍掉它。

  • 使用“ends-with”模式search(SELECT * FROM mytable WHERE phonenumber LIKE'blah%')执行数据库search。 这将处理没有提供区号的场所(尽pipe有可能出错),但是你的数据库有区号的号码。

作为主要.Net解决scheme的一个替代scheme,根据类似问题的答案进行了改编:

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

通过外观的东西,你试图抓住任何10位数的电话号码….

为什么不做一个stringreplace文本首先删除任何下列字符。

 <SPACE> , . ( ) - [ ] 

然后,你可以做一个10位数字的正则expression式search。

 \d{10} 

你有没有通过regexlib 电话号码类别 。 看起来好像很多人都在做你所需要的东西。

关于理查德的观点,这个代码将处理大部分关于分机号码的问题,而美国的国家代码(+1)则被预设了。

不是最优雅的解决scheme,但我必须快速解决问题,所以我可以继续我在做什么。

我希望它可以帮助别人。

  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