不区分大小写的正则表达式-VBA [英] Case-insensitive Regular Expression - VBA
问题描述
背景:
现在,我正在回答一个问题,在 VBA
中使用 RegEx
玩耍。目的是创建一个字符串中存在的名称列表。 RegEx
是首选解决方案,因为我们要防止 VBA
绊倒看起来类似的标点符号和子字符串,例如: Jack
或 Jacky
。
Just now, I was answering a question and was playing around with RegEx
within VBA
. The goal is to create a list of names that exist within a string. RegEx
was the go-to solution since we want to prevent VBA
to stumble over punctuation marks and substrings that look similar e.g.: Jack
or Jacky
.
样本数据:
让我给一个简单样本。假设我们有一个像这样的字符串:
Let me give a simple sample. Imagine we have a string like:
Dim str As String: str = "Jack's turn, Becky's or Frank?"
我们想知道字符串中提到某个数组中的哪些名称,例如:
we want to know which names in a certain array are mentioned within the string, for example:
Dim arr As Variant: arr = Array("Jack", "Frank")
示例代码:
为了防止在数组上进行迭代,我使用了以下代码:
To prevent an iteration over the array, I went with the following code:
Sub Test()
Dim str As String: str = "Jack's turn, Becky's or Frank?"
Dim arr As Variant: arr = Array("Jack", "Frank", "Beth")
Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\b(" & Join(arr, "|") & ")\b"
regex.Global = True
Set hits = regex.Execute(str)
For Each hit In hits
Debug.Print hit
Next hit
End Sub
问题:
尽管以上内容会巧妙地返回两个匹配,但不区分大小写。例如,更改以下行将仅返回 Jack
:
Whereas the above would neatly return the two hits, it would not work case-insensitive. For example, changing the following line will only return Jack
:
Dim str As String: str = "Jack's turn, Becky's or frank?"
我想我可以通过使用(? i)
:
regex.Pattern = "(?i)\b(" & Join(arr, "|") & ")\b"
但是问题是对于大多数语言(在此处测试),该方法非常适用,但是 VBA
似乎有问题,并在执行时生成错误5017
。
But the problem is that this would work perfectly for most languages (test here), however VBA
seems to have a problem with it and generates an Error 5017
upon execution.
问题:
有人吗知道为什么? VBA
不支持此功能吗?还是我的语法错误?如果不支持,那么使匹配不区分大小写,同时又保留 Join
名称数组的可能性的替代方法是什么呢?
Does anybody know why? Is this not supported within VBA
or is my syntax wrong? If not supported, what is the alternative to get hits case-insensitive while retaining the possibility to Join
the array of names?
奖金问题:
最终我想通过定界符将$ c $ Hit
加入,例如:
Ultimately I would like to Join
the Hits
together through a delimiter, for example like:
Debug.Print Join(regex.Execute(str),", ")
但是,我意识到执行会返回一个集合,并且我首先需要避免迭代。
However, I realized execution returns a collection and needs iteration first which I would like to avoid.
推荐答案
Set RegExp对象上的属性,即
Set the property on the RegExp object i.e.
regex.ignorecase = True
RegExp对象具有三个影响常规
表达式应用方式的属性:
The RegExp object has three properties that affect how regular expressions are applied:
IgnoreCase:我相信这是不言而喻的
IgnoreCase: I trust this is self-explanatory
Global:确定
是否在输入字符串中找到所有可能的匹配项。如果
Global设置为false,则将仅找到第一个匹配项或替换
(如适用)。
Global: Determines whether or not to find all possible matches in the input string. If Global is set to false, only the first match will be found or replaced, as applicable.
MultiLine:确定匹配项是否可以跨越
MultiLine: Determines whether matches can span accross line breaks.
另请参见: https://docs.microsoft.com/zh-cn/previous-versions//1400241x%28v%3dvs.85%29
,然后从 https://www.regular-expressions.info/关于vbscript.html 关于VBScript正则表达式支持的信息,因为我们正在使用Microsoft VBScript正则表达式5.5
And from https://www.regular-expressions.info/vbscript.html regarding VBScript’s Regular Expression Support as we are using Microsoft VBScript Regular Expressions 5.5
没有模式修饰符可在正则表达式中设置匹配选项。
No mode modifiers to set matching options within the regular expression.
这篇关于不区分大小写的正则表达式-VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!