谢谢您的回复。
我觉得您看代码的话可能反而会更费劲些:)
代码如下:
Imports System.Text.RegularExpressions
Private m_Constants As Collection
Private Const Number As String = "(\-?\d+\.?\d*)" '匹配数字,含符号和小数点
Private Const cStrings As String = "\w+" '"[a-zA-Z][u4e00-u9fa5]+"'匹配词或英文单词
Private Const AnyStrings As String = "(.+)" '表示匹配任何字符
Private AnyStringsWithParenthesis As String = "(""{1}" & AnyStrings & """{1})" '表示任何字符(字符串有双引号括着)
Private NumberOrAnyStringsWithParenthesis = Number & "|" & AnyStringsWithParenthesis
Private NumberOrAnyStringsWithParenthesisUnCapture = Number.Insert(1, "?:") & "|" & AnyStringsWithParenthesis.Insert(1, "?:")
Private RegexStringAnd As Regex
RegexStringAnd = New Regex("(?<A>\s*" & NumberOrAnyStringsWithParenthesisUnCapture & ")(?<Operater>\s*[&]{1}\s*)(?<B>" & NumberOrAnyStringsWithParenthesisUnCapture & "\s*)") '定义&运算符,&的两边必须是字符串(字符串有双引号括着)或数字
Private Function DoStringAnd(ByVal m As Match) As String
Select Case m.Groups(4).Value.Trim(" "c)
Case "&"
Return """"c & m.Groups(1).Value.ToString & m.Groups(2).Value.ToString & """"c
Case Else
Throw New Exception("没有匹配的预定义运算符")
End Select
End Function
ExpressionString = "16& ""-"" & ""1"""
Do While RegexStringAnd.IsMatch(ExpressionString)
ExpressionString = RegexStringAnd.Replace(ExpressionString, AddressOf DoStringAnd)
Debug.Print(ExpressionString)
Loop
Debug返回的结果是"""-"" &""1"""
希望输出的结果是"16-1",问题出在匹配双引号上,DoStringAnd函数的m.Groups匹配到{}、{-"" &""1}、{16}、{& }、{"-" &"1"},5个部分
编程是永无止境的,向大家学习