帮助验证Pascal语言中输入过程的正确性。请。
标识符,用双引号括起来的字符串常量和用单引号括起来的单个字符可以用作参数。
真正
proc1('s',13,"sss");
proc2('s',s,d,11,"sss");
proc3("sss");
假
proc1(s',11,"sss");
proc2('s',s,d,11,sss");
proc3("sss);
proc4("sss";
我的可怜尝试:
public void ThreadPoolCallback(Object threadContext)
{
if ((str.Length == 0) && (str[str.Length - 1] != ')'))
{
haveError = true;
}
else
{
int startIndex = str.IndexOf('('),
lastIndex = str.LastIndexOf(')'),
startIndex2 = str.LastIndexOf('('),
lastIndex2 = str.IndexOf(')');
if (startIndex < lastIndex && startIndex > 0 && lastIndex == str.Length - 1 &&
startIndex == startIndex2 && lastIndex == lastIndex2)
{
int curr = startIndex + 1;
while (curr < lastIndex)
{
string s = "";
while (str[curr] != ',' && curr < lastIndex)
{
s += str[curr];
curr++;
}
curr++;
}
}
else
{
haveError = true;
}
}
doneEvent.Set();
}
我没有将整个Pascal引擎构建到这个正则表达式中,但它应该做你想要的:
@"\((?:(?:'[\d\w_]+'|""[\d\w_]*""|[\d\w_]+),?)*\);"
它将匹配起始的圆括号'(
'数字和由单引号或双引号(匹配对)或字母和数字后跟可选逗号',
'并以结束圆括号')
'和分号';
'结尾的字母匹配。
如何使用:
string test = "proc1('s',13,"sss");\n" + "proc2('s',s,d,11,"sss");\n"
+ "proc3("sss");" + "proc1(s',11,"sss");" + "proc2('s',s,d,11,sss");"
+ "proc3("sss);" + "proc4("sss";";
Regex regex = new Regex(@"\((?:(?:'[\d\w_]+'|""[\d\w_]*""|[\d\w_]+),?)*\);");
foreach (Match match in regex.Matches(test))
{
Console.Write(match.Value);
}