用于收集括号 () 之间的值的正则表达式

Regular Expression for collecting the values between brackets ()

我有这样的字符串:

((8 eq 'null' or 9 ne 'null') and (1 eq 'uday' ) and (2 eq 'kumar')))

我想先获取 8 eq 'null' or 9 ne 'null' 并替换为 true 或 false,然后获取 1 eq 'uday' 并替换为 true 或 false,然后是 2 eq 'kumar' 并替换为 true 或 false。我最终解决的表达式应该看起来像 ((false or true) and (true) and (false))) or (false and true and false).

任何人都可以建议我使用正则表达式或任何其他概念的 VBScript 解决方案吗?

定义一个带有捕获组的正则表达式,匹配括号之间不是括号的所有内容:

\(([^()]*)\)

\(...\) 匹配文字左括号和右括号。 [^()]* 是一个字符 class,它匹配除括号之外的所有内容零次或多次 (*)。字符 class 周围的括号是一个捕获组,它允许通过 SubMatches 集合在没有括号的情况下提取括号之间的文本。

s = "((8 eq 'null' or 9 ne 'null') and (1 eq 'uday' ) and (2 eq 'kumar')))"

Set re = New RegExp
re.Pattern = "\(([^()]*)\)"
re.Global  = True   'find all matches in a string, not just the first one

For Each m In re.Execute(s)
    WScript.Echo m.SubMatches(0)
Next

输出:

8 eq 'null' or 9 ne 'null'
1 eq 'uday'
2 eq 'kumar'

VBScript 无法为您计算提取的(子)表达式,因此您需要为此编写更多代码。