Excel VBA 正则表达式替换函数替换文字 $1
Excel VBA RegEx Replace Function Substituting a Literal $1
我经常依靠 VBA 中简单易用的 Replace 函数来进行简单的字符串替换,但长期以来我一直被正则表达式的神奇魅力所吸引,可以执行更复杂的字符串操作。但在我的实验中,我只是坚持认为我的替换值“$1”作为输出字符串的文字部分返回,而不是作为与 RegEx 模式匹配的文本返回。我认为我做错的事情很简单,但我看不到。谁能提供一些指导?
我已将 Microsoft VBScript Regular Expressions 5.5 库作为参考包含在我的 VBA 项目中。这是我的代码的简化片段:
Dim regEx As RegExp
Dim strInput As String
Dim strPattern As String
Dim strReplace As String ' I've tried type Variant also
strPattern = "/[a-z]" ' Find strings with a forward slash followed by a lowercase letter; this works
strReplace = "" ' I've also tried using this value directly in the Replace function without first assigning it to a string value.
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
strInput = regEx.Replace(strInput, strPattern)
End If
如果我的输入字符串类似于 "High/low Value",则结果将是 "Highow Value",而我所追求的是 "High/Low Value"。我很难过。有什么想法吗?
如果您在您的模式中使用捕获组,但您没有使用,请使用“$1”。
根据所提供的信息,这应该可以工作,并且会转换匹配模式的多个实例。
Sub x()
Dim regEx As RegExp
Dim strInput As String
Dim strPattern As String
Dim strReplace As String ' I've tried type Variant also
Dim i As Long, f, s As String
Set regEx = New RegExp
strPattern = "/[a-z]" ' Find strings with a forward slash followed by a lowercase letter; this works
strInput = "High/low and Low/high"
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
If .Test(strInput) Then
s = strInput
For i = 0 To .Execute(strInput).Count - 1
f = .Execute(strInput)(i).FirstIndex
s = Left(s, f) & UCase(.Execute(strInput)(i)) & Right(s, Len(s) - f - 2)
Next i
strInput = s
MsgBox strInput
End If
End With
End Sub
我经常依靠 VBA 中简单易用的 Replace 函数来进行简单的字符串替换,但长期以来我一直被正则表达式的神奇魅力所吸引,可以执行更复杂的字符串操作。但在我的实验中,我只是坚持认为我的替换值“$1”作为输出字符串的文字部分返回,而不是作为与 RegEx 模式匹配的文本返回。我认为我做错的事情很简单,但我看不到。谁能提供一些指导?
我已将 Microsoft VBScript Regular Expressions 5.5 库作为参考包含在我的 VBA 项目中。这是我的代码的简化片段:
Dim regEx As RegExp
Dim strInput As String
Dim strPattern As String
Dim strReplace As String ' I've tried type Variant also
strPattern = "/[a-z]" ' Find strings with a forward slash followed by a lowercase letter; this works
strReplace = "" ' I've also tried using this value directly in the Replace function without first assigning it to a string value.
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
strInput = regEx.Replace(strInput, strPattern)
End If
如果我的输入字符串类似于 "High/low Value",则结果将是 "Highow Value",而我所追求的是 "High/Low Value"。我很难过。有什么想法吗?
如果您在您的模式中使用捕获组,但您没有使用,请使用“$1”。
根据所提供的信息,这应该可以工作,并且会转换匹配模式的多个实例。
Sub x()
Dim regEx As RegExp
Dim strInput As String
Dim strPattern As String
Dim strReplace As String ' I've tried type Variant also
Dim i As Long, f, s As String
Set regEx = New RegExp
strPattern = "/[a-z]" ' Find strings with a forward slash followed by a lowercase letter; this works
strInput = "High/low and Low/high"
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
If .Test(strInput) Then
s = strInput
For i = 0 To .Execute(strInput).Count - 1
f = .Execute(strInput)(i).FirstIndex
s = Left(s, f) & UCase(.Execute(strInput)(i)) & Right(s, Len(s) - f - 2)
Next i
strInput = s
MsgBox strInput
End If
End With
End Sub