我正在尝试替换撇号之间的文本的一部分,而不是全部,只是一部分。例如,我需要将仅在引号之间的文本内的字符/ *和* /替换为空文本,而不是在其外部。
我的输入文字,例如:
A = 'THIS IS AN ALPHABETIC /* CONSTANT' || WS_CON1 /* */ || 'TEST STRING */';
Run Code Online (Sandbox Code Playgroud)
预期产量:
A = 'THIS IS AN ALPHABETIC CONSTANT' || WS_CON1 /* */ || 'TEST STRING ';
Run Code Online (Sandbox Code Playgroud)
我提取了引号中的文本,但不知道如何用空文本替换/ *和* /。
子ReplaceWithRegex()
Dim strPattern作为字符串
昏暗的strReplace作为字符串
昏暗regEx作为变体
昏暗的strtxt作为字符串
设置regEx = CreateObject(“ vbscript.regexp”)
strtxt =“ A ='这是字母//常数'|| WS_CON1 / * * / ||'测试字符串* /';”
strPattern =“ \'([^ \'] *)\'”
strReplace =“”
使用正则表达式
.Global =真
.MultiLine =真
.IgnoreCase =假
.Pattern = strPattern
结束于
如果regEx.Test(strtxt)然后
Debug.Print regEx.Replace(strtxt,strReplace)
其他
MsgBox(“不匹配”)
万一
结束子
显然,这会将引号之间的所有文本替换为空字符串。
我该如何解决这个问题?
This expression might help you to replace those undesired /*:
[A-Z]\s\/\*\s[A-Z]
Run Code Online (Sandbox Code Playgroud)
We can simply wrap that in a capturing groups (), similar to:
([A-Z])\s(\/\*)\s([A-Z])
Run Code Online (Sandbox Code Playgroud)
Then, we can replace it with $1 $3 and ignore the second undesired capturing group:
This tool helps you to modify/change/edit your expressions, as you wish.
This link helps you to visualize your expressions:
If you may have more patterns in these capturing groups, you can simply add them using an |, such as:
([A-Z])\s(\/\*|\*\/)(\s[A-Z]|\x27)
Run Code Online (Sandbox Code Playgroud)
You might also want to use \x27 instead of ' such that your code would become easy to read.
[A-Z]\s\/\*\s[A-Z]
Run Code Online (Sandbox Code Playgroud)
This snippet returns the runtime of a 1-million times for loop.
([A-Z])\s(\/\*)\s([A-Z])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
128 次 |
| 最近记录: |