我正在尝试替换撇号之间的文本的一部分,而不是全部,只是一部分。例如,我需要将仅在引号之间的文本内的字符/ *和* /替换为空文本,而不是在其外部。
我的输入文字,例如:
A = 'THIS IS AN ALPHABETIC /* CONSTANT' || WS_CON1 /* */ || 'TEST STRING */';
预期产量:
A = 'THIS IS AN ALPHABETIC CONSTANT' || WS_CON1 /* */ || 'TEST STRING ';
我提取了引号中的文本,但不知道如何用空文本替换/ *和* /。
Sub ReplaceWithRegex() Dim strPattern作为字符串 Dim strReplace作为字符串 Dim regEx作为变量 Dim strtxt作为字符串 集regEx = CreateObject(“ vbscript.regexp”) strtxt =“ A ='这是一个字母/ *常量'|| WS_CON1 / * * / ||'测试字符串* /';“ strPattern = “\ '([^ \'] *)\'” strReplace = “” 随着正则表达式 。全球=真 .MultiLine =真 .IgnoreCase =假 .Pattern = strPattern 结束随着 如果regEx.Test(strtxt)然后 调试。打印regEx.Replace(strtxt,strReplace) 其他 MsgBox(“ 结束子
显然,这会将引号之间的所有文本替换为空字符串。
我该如何解决这个问题?
这是使用正则表达式的另一种VBA方法。
Option Explicit
'Set Reference to Microsoft VBScript Regular Expressions 5.5
Function reReplaceComment(S As String) As String
Dim RE As RegExp
Dim I As Long, J As Long
Set RE = New RegExp
With RE
.Global = True
.Pattern = "('[^']*?)(?:(?:/\*)|(?:\*/))([^']*?')"
reReplaceComment = .Replace(S, "$1$2")
End With
End Function
('[^']*?)(?:(?:/\*)|(?:\*/))([^']*?')
('[^']*?)
'
[^']*?
(?:(?:/\*)|(?:\*/))
(?:/\*)
(?:/\*)
/
\*
(?:\*/)
(?:\*/)
\*
/
([^']*?')
[^']*?
'
$ 1 $ 2
用RegexBuddy创建
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句