使用正则表达式查找数字
Finding a Digit using Regex
我尝试使用 Regex Vbscript 查找数字,我使用 \d{1}
或 \d
,它从单词或日期中查找任何数字,我只是不想从日期或字符串中提取,就一个数字,数字和圆点周围没有任何东西(.
),如何找到它?
示例
12/12/2009 - No need to match
09 - no need to match
8. match
7 match
谁能帮帮我?
您可以像这样使用正则表达式:
(^|[\b\s])\d\b
如果您的规格是 "match strings that consists of exactly one digit",请使用“^\d$”(字符串开头 - 一位数字 - 字符串结尾)- 如
>> Set r = New RegExp
>> r.Pattern = "^\d$"
>> For Each s In Split("12/12/2009 09 8 7 a7b")
>> WScript.Echo s, CStr(r.Test(s))
>> Next
>>
12/12/2009 False
09 False
8 True
7 True
a7b False
如果没有,请明确描述您的规格。
更新:
因为我确实错过了“8”中的点。并混合 "contain" 和 "consist",我为 "match strings that contain exactly one digit":
输入 "^\D*\d\D*$"
>> r.Pattern = "^\D*\d\D*$"
>> For Each s In Split("2/1/2009 09 8. 7 a7b")
>> WScript.Echo s, CStr(r.Test(s))
>> Next
>>
2/1/2009 False
09 False
8. True
7 True
a7b True
我尝试使用 Regex Vbscript 查找数字,我使用 \d{1}
或 \d
,它从单词或日期中查找任何数字,我只是不想从日期或字符串中提取,就一个数字,数字和圆点周围没有任何东西(.
),如何找到它?
示例
12/12/2009 - No need to match
09 - no need to match
8. match
7 match
谁能帮帮我?
您可以像这样使用正则表达式:
(^|[\b\s])\d\b
如果您的规格是 "match strings that consists of exactly one digit",请使用“^\d$”(字符串开头 - 一位数字 - 字符串结尾)- 如
>> Set r = New RegExp
>> r.Pattern = "^\d$"
>> For Each s In Split("12/12/2009 09 8 7 a7b")
>> WScript.Echo s, CStr(r.Test(s))
>> Next
>>
12/12/2009 False
09 False
8 True
7 True
a7b False
如果没有,请明确描述您的规格。
更新:
因为我确实错过了“8”中的点。并混合 "contain" 和 "consist",我为 "match strings that contain exactly one digit":
输入 "^\D*\d\D*$">> r.Pattern = "^\D*\d\D*$"
>> For Each s In Split("2/1/2009 09 8. 7 a7b")
>> WScript.Echo s, CStr(r.Test(s))
>> Next
>>
2/1/2009 False
09 False
8. True
7 True
a7b True