我有 RF123456789、RF1234567890 等记录。 我只想匹配以“RF”开头的记录,后面紧跟 9 位数字。 如果大于 9 位或小于 9 位应显示 Invaid。 我写了下面的代码,但问题是,如果数字大于 9,它也显示有效。 我知道我写信只是为了检查它是否以 RF 开头并后跟 9 位数 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在尝试使用正则表达式删除特定的文本字符串,并具有以下适用于 regex101.com 但是我知道 VBA 在使用正则表达式时有一些细微差别,而我的模式似乎根本不起作用。
我验证的模式在下面的代码中,如果可能的话,我还想将它与下面的代码结合起来(旨在删除 html 标签)“\<.*?>”这确实有效,但我目前运行一个单独的 regex.replace function 删除它。
Dim regEx As Object , str As String
Set regEx = CreateObject("VBScript.RegExp")
With regEx
'.Pattern = "<a\b([^>""']|""[^""]*""|'[^']*')+class=""changed-by"">.*?<\/a>"
'.pattern = "\<.*?\>" ' this is the second pattern i use to remove html tages which works
.Global = True 'If False, would replace only first
.IgnoreCase = False
.MultiLine = False
End With
str = some text here <a href="/instrument/2014/36.pdf" target="_blank" title="2014/36 - 01/07/2014" class="changed-by">1</a> some text here
Debug.Print regEx.Replace(str, "")
如果您要使用Replace
function,我认为您需要一些 output 参数,将您找到的内容替换为""
将始终返回""
。 这就是我想出的,我不确定 Regex 是否真的比你的更好,但它适用于 VBA。 主要区别是我添加的第一个和最后一个组以提取所有文本。
Dim RegEx As New RegExp
Dim Value As String
RegEx.IgnoreCase = True
RegEx.Pattern = "(.*?)<a\b(?:""[^""]*?""|'[^']*?'|[^>]*?)+>([^<]*?)<\/a\b.*?>(.*)"
Value = "some text here <a href=""/instrument/2014/36.pdf"" target=""_blank"" title=""2014/36 - 01/07/2014"" class=""changed-by"">1</a> some text here"
Debug.Print RegEx.Replace(Value, "$1$2$3")
另一个选项是您只需使用Execute
function 并将文本从相应的组中拉出。
Dim Col As MatchCollection
Dim i As Long
Set Col = RegEx.Execute(Value)
With Col
For i = 0 To .Count - 1
With .Item(i)
Debug.Print .SubMatches(0) & .SubMatches(1) & .SubMatches(2)
End With
Next
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.