【发布时间】:2010-10-25 01:45:59
【问题描述】:
我需要一个可以在 VBScript 和 .NET 中使用的正则表达式,它只返回在字符串中找到的数字。
例如,以下任何“字符串”应仅返回 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
这将在电子邮件解析器中用于查找客户可能在电子邮件中提供的电话号码并进行数据库搜索。
我可能错过了一个类似的正则表达式,但我确实在 regexlib.com 上进行了搜索。
[编辑] - 添加由 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
}
【问题讨论】:
-
正如我所说,\D 比 ^\d 简单。
标签: c# vb.net regex vbscript code-generation