我有三个字符串,如
string a = "[ERROR] = any thing And [ID] > 20 And [NAME] like Varun";
string b = "[AGE] < 60 Or [ERROR] like Exception And [ID] > 20 And [NAME] like Varun And [ERROR] = any thing ";
string c = "[ID] > 20 And`enter code here` [NAME] like Varun And [ERROR] = any thing";
我想删除从[ERROR]开始到下一个运算符的字符串,如And / Or .....
我想要答案
string a = "[ID] > 20 And [NAME] like Varun";
string b = "[AGE] < 60 And [ID] > 20 And [NAME] like Varun";
string c = "[ID] > 20 And [NAME] like Varun";
如何删除.. ???
答案 0 :(得分:2)
如果你对正则表达式不满意(我假设你不是),你可以使用这个伪代码:
将字符串拆分为单词(以空格分隔)
扫描单词列表,如果它等于[ERROR]
,则跳过所有单词,直至下一个单词,以[
开头,以]
结尾
如果单词以[
开头并以]
结尾,则将其添加到输出中以及所有后续单词不以[
开头并以]
。
public static String FilterString(string value)
{
string[] lsWords = value.Split(' ');
StringBuilder lsbBuilder = new StringBuilder();
bool lbIncluding = true;
foreach(String lsWord in lsWords)
{
if (lsWord.StartsWith("[") && lsWord.EndsWith("]"))
lbIncluding = (lsWord != "[ERROR]");
if (lbIncluding)
{
if (lsbBuilder.Length > 0) lsbBuilder.Append(' ');
lsbBuilder.Append(lsWord);
}
}
return lsbBuilder.ToString();
}
...
string a = "[ERROR] = any thing And [ID] > 20 And [NAME] like Varun";
string b = "[AGE] < 60 Or [ERROR] like Exception And [ID] > 20 And [NAME] like Varun And [ERROR] = any thing ";
string c = "[ID] > 20 And`enter code here` [NAME] like Varun And [ERROR] = any thing";
a = FilterString(a);
b = FilterString(b);
c = FilterString(c);
这是输出:
[ID] > 20 And [NAME] like Varun
[AGE] < 60 Or [ID] > 20 And [NAME] like Varun And
[ID] > 20 And`enter code here` [NAME] like Varun And
这并没有摆脱ANDS / ORS等等,你需要自己做那个,但这应该给你一个很好的起点。