gpt4 book ai didi

regex - VBscript:如何在引号('s)之外的字符串中将文本的特定部分大写

转载 作者:行者123 更新时间:2023-12-01 22:36:28 51 4
gpt4 key购买 nike

我需要你的帮助。在 vbscript 中,我有一个字符串,例如

s = 'Abc' 和 'Def' 或 'Ghin' 在 'jkl' 中而不是 'mnoR' 并且 ... 或 ... NOT ... iN ...

我想将这些特定的 4 个运算符(以小写和大写文本的任意组合)大写:and、or、in、not 为大写。

这些运算符存在于引号 (') 之外 - 因为它们之间有业务规则。这很重要,因为正如您在第三条规则(“Ghin”)中看到的那样,我在规则名称中使用了“in”,在这些情况下,我不希望更改引号(业务规则名称)之间的文本.

我如何在 vbscript 中解决这个问题,最好使用 RegEx?

TIA

编辑:感谢您的帮助。

抱歉,但我忘了提及一个细节:我可以在引号外添加文本,即“(”、“)”、“[”、“]”或条件“1 = 1”,但同样:要更改的运算符存在于引号外,引号内什么都不做。

使用前面的例子:s = "('abc' and ['Def' Or 'Ghin'] In 'jkl' not 1=1 AND 'mnoR' And 'pqr' or 'xyz' NOT 'lmn' iN 'Opq')"

s = "('abc' AND ['Def' OR 'Ghin'] IN 'jkl' NOT 1=1 AND 'mnoR' AND 'pqr' OR 'xyz' NOT 'lmn' IN 'Opq')"

最佳答案

在其他语言中,您可以使用奇特的环视模式(逻辑上)仅将正则表达式应用于部分输入,在 VBScript 中,您应该使用带状态的正则表达式替换函数或 Split()。

第一个替代方案的演示脚本:

Dim gb_magic : gb_magic = True
Function gf_magic(sMatch, nPos, sSrc)
gf_magic = sMatch
If "'" = sMatch Then
gb_magic = Not gb_magic
Else
If gb_magic Then
gf_magic = UCase(sMatch)
End If
End If
End Function

Dim s : s = "s = 'Abc and def' and 'not Def' Or 'Ghin' In 'jkl in or' not 'mnoR'"
WScript.Echo s
Dim r : Set r = New RegExp
r.Global = True
r.IgnoreCase = True
r.Pattern = "and|not|or|in|'"
gb_magic = True
s = r.Replace(s, GetRef("gf_magic"))
WScript.Echo s

输出:

s = 'Abc and def' and 'not Def' Or 'Ghin' In 'jkl in or' not 'mnoR'
s = 'Abc and def' AND 'not Def' OR 'Ghin' IN 'jkl in or' NOT 'mnoR'

关于regex - VBscript:如何在引号('s)之外的字符串中将文本的特定部分大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22451989/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com