我有RF123456789,RF1234567890等记录.
I have records like RF123456789, RF1234567890 etc.
我只想匹配以'RF'开头的记录,后面紧跟9位数字.如果大于9位或小于9位,则应显示Invaid.我已经写了下面的代码,但是问题是,如果数字大于9,那么它也显示有效.我了解我已经写过检查,仅检查它是否以RF开头,然后是9位数字,因此,如果是10位数字,则显然与我的模式匹配.有什么办法可以将它限制为仅9位数字而不是10位数字?
I just want to match the records which are starting with 'RF' followed by exactly 9 digits of number. If it is more than 9 digit or less than 9 digit It should show Invaid. I have wrote the below code, but the problem with this is , if the number is more than 9 also it is showing valid. I understand that I have written to check only if it starts with RF and followed by 9 digits, so in case of 10 digits it is obviously matching my pattern. Is there any way I could restrict it for only 9 digits rather that 10?
Set myrange = Range("C2:C" & Rng) For Each c In myrange strinput = c.Value patn = "([r|R][f|F][0-9]{9})" If patn <> "" Then With regex .Global = True .MultiLine = True .IgnoreCase = False .Pattern = patn End With If regex.Test(strinput) Then c.Offset(0, 5) = "Valid" Else c.Offset(0, 5) = "Invalid" End If End If ''checking Column D and E are matching or not'' If c.Offset(0, 1) <> "" Then If c.Offset(0, 1) = c.Offset(0, 2) Then c.Offset(0, 6) = "Matching" Else c.Offset(0, 6) = "Not Matching" End If Else c.Offset(0, 6) = "Empty" End If Next推荐答案
您根本不应该使用正则表达式.一个简单的Like语句就可以.
You shouldn't use regex for this at all. A simple Like statement would do.
只需使用strinput Like "RF#########".如果以RF(不区分大小写)开头,然后有9位数字,则返回true,否则返回false.
Just use strinput Like "RF#########". That returns true if it starts with RF (case insensitive) and then has 9 digits, else false.
更多推荐
VBA正则表达式匹配模式
发布评论