VBA正则表达式匹配模式

编程入门 行业动态 更新时间:2024-10-27 16:28:06
本文介绍了VBA正则表达式匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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正则表达式匹配模式

本文发布于:2023-11-29 17:45:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   正则表达式   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!