我有一个字符串,其中包含一堆函数调用。我需要提取每次出现的VariableSet函数调用。功能可以按任何顺序出现。这是一个例子:
parsedExpression = "VariableSet(b, 999)If(a = 0,"Black",SetColor(a,b,c))VariableSet("a" ,1.573) VariableSet( c,-2387)"
我需要找到以" VariableSet开头的每个匹配("并以其后面的第一个右括号结束。所以,对于上面的例子,我需要一个这样的列表:
VariableSet(b, 999)
VariableSet("a" ,1.573)
VariableSet( c,-2387)
我打算使用下面的代码,但我无法确定正确的正则表达式模式。我能想到的最好的是" VariableSet(。*(?i:)\ b)"但它不会产生上面的列表。
Dim matches As MatchCollection = Regex.Matches(parsedExpression, "VariableSet\(.*(?i:\)\b)")
' Loop over matches.
For Each m As Match In matches
' Loop over captures.
For Each c As Capture In m.Captures
Dim varName As String = ""
Dim varValue As String = ""
Dim firstCommaPosition As Integer
'For every VariableSet that was found do the following:
'Parse the captured string to get the variable name and value
varName = c.Value.Replace("VariableSet(", "").Replace(")", "")
firstCommaPosition = varName.IndexOf(",")
varValue = varName.Substring(firstCommaPosition + 1)
varName = varName.Substring(0, firstCommaPosition).Replace("""", "")
'Set the variable
ce.Variables(varName) = ce.Evaluate(varValue)
'Remove this instance of VariableSet() function from parsedExpression
parsedExpression = parsedExpression.Replace(c.Value, "")
Next
Next
如果有人能提供正确的正则表达式模式,我将不胜感激。
答案 0 :(得分:0)
也许这会对你有所帮助:
Dim strMatch As String = ""
Dim strVar1 As String = ""
Dim strVar2 As String = ""
Dim strExpression As String = "VariableSet(b, 999)If(a = 0,""Black"",SetColor(a,b,c))VariableSet(""a"" ,1.573) VariableSet( c,-2387)"
Dim rx As New RegularExpressions.Regex("VariableSet\((?<V1>.*?),(?<V2>.*?)\)", RegularExpressions.RegexOptions.IgnoreCase)
Dim rxMatch As RegularExpressions.MatchCollection = rx.Matches(strExpression)
For intI As Integer = 0 To rxMatch.Count - 1
strMatch = rxMatch(intI).Value 'VariableSet(b, 999)
strVar1 = rxMatch(intI).Groups("V1").ToString 'b
strVar2 = rxMatch(intI).Groups("V2").ToString ' 999
Next
答案 1 :(得分:0)
VariableSet\([^)]*\)
应该是直接替代品
如果您想获得想象力,可以使用单个正则表达式完成所有代码。
# VariableSet\((\s*"?\s*([^,")]*?)\s*"?\s*(?:,\s*"?\s*([^,")]*?)\s*"?\s*)?)\)
VariableSet
\( # Open paren
( # (1 start), Inside paren's
\s*
"? \s*
( [^,")]*? ) # (2), Var
\s*
"? \s*
(?:
, # Comma
\s*
"? \s*
( [^,")]*? ) # (3), Value
\s*
"? \s*
)?
) # (1 end)
\) # Close paren
示例输入字符串:
VariableSet(b, 999)
VariableSet("a" ,1.573)
VariableSet( c,-2387)
VariableSet( , 999)
VariableSet( "aadsfasdf")
VariableSet( )
输出匹配(Var / Value):
** Grp 2 - ( pos 12 , len 1 )
b
** Grp 3 - ( pos 16 , len 3 )
999
----------------
** Grp 2 - ( pos 35 , len 1 )
a
** Grp 3 - ( pos 40 , len 5 )
1.573
----------------
** Grp 2 - ( pos 63 , len 1 )
c
** Grp 3 - ( pos 65 , len 5 )
-2387
----------------
** Grp 2 - ( pos 86 , len 0 ) EMPTY
** Grp 3 - ( pos 88 , len 3 )
999
----------------
** Grp 2 - ( pos 108 , len 9 )
aadsfasdf
** Grp 3 - NULL
----------------
** Grp 2 - ( pos 136 , len 0 ) EMPTY
** Grp 3 - NULL