【发布时间】:2016-01-21 19:27:24
【问题描述】:
我正在尝试使用 Regex for Excel 替换字符串中的子字符串“AV-”。我创建了以下函数,但它没有找到或替换匹配项
Sub simpleRegex()
Dim strPattern As String: strPattern = "[AV-]{AV-}"
Dim strReplace As String: strReplace = "ZS-"
Dim regEx As New RegExp
Dim strInput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("E" & (ActiveCell.Row))
If strPattern <> "" Then
strInput = Myrange.Value
strReplace = ""
With regEx
.Global = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
ActiveSheet.Range("E" & (ActiveCell.Row)).Value = (regEx.Replace(strInput, strReplace))
Else
MsgBox ("Not matched")
End If
End If
结束子
【问题讨论】:
-
我假设您正在尝试学习一些东西,因为这可以通过 vba 中的一行来完成
Myrange.Replace "AV-", "ZS-", xlPart, , True -
很难说输入/输出要求是什么,但您可以使用
ResultString = Regex.Replace(SubjectString, "(AV-)(.*)", "ZS-$2-TRAILING", RegexOptions.Multiline)之类的东西或 Scott 提到的东西。