正则我也不会,只好继续提供笨方法,这回括号里有括号可以解决了 int i = str1.IndexOf(".PID("); int j=0; while (i >= 0) { i = i + 4; int x = 0; for (int n = i + 1; n < str1.Length; n++) { if (str1[n] == '(') x++; if (str1[n] == ')') { if (x == 0) { j = n; break; } x--; } } if(j>i) str1 = str1.Substring(0, i - 1) + "[" + str1.Substring(i + 1, j - i - 1) + "]" + str1.Substring(j + 1); i = str1.IndexOf(".PID(",i+1); }
int i = str1.IndexOf('(');
int j = str1.IndexOf(')', i + 1);
while (i >= 0 && j > i)
{
str1 = str1.Substring(0, i - 1) + "[" + str1.Substring(i + 1, j - i - 1) + "]" + str1.Substring(j + 1);
i = str1.IndexOf('(');
j = str1.IndexOf(')', i + 1);
}
int i = str1.IndexOf(".PID(");
int j = str1.IndexOf(')', i + 1);
while (i >= 0 && j > i)
{
i = i + 4;
str1 = str1.Substring(0, i - 1) + "[" + str1.Substring(i + 1, j - i - 1) + "]" + str1.Substring(j + 1);
i = str1.IndexOf(".PID(");
j = str1.IndexOf(')', i + 1);
}
int i = str1.IndexOf(".PID(");
int j=0;
while (i >= 0)
{
i = i + 4;
int x = 0;
for (int n = i + 1; n < str1.Length; n++)
{
if (str1[n] == '(')
x++;
if (str1[n] == ')')
{
if (x == 0)
{
j = n;
break;
}
x--;
}
}
if(j>i)
str1 = str1.Substring(0, i - 1) + "[" + str1.Substring(i + 1, j - i - 1) + "]" + str1.Substring(j + 1);
i = str1.IndexOf(".PID(",i+1);
}