0

我有三个字符串

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";

我怎样才能删除..???

4

1 回答 1

2

如果您对正则表达式不满意(我假设您不满意),您可以使用以下伪代码:

  1. 将字符串拆分为单词(以空格分隔)

  2. 扫描单词列表,如果相等[ERROR]则跳过所有单词直到下一个单词开头[和结尾]

  3. 如果单词以开头[和结尾,]则将其添加到输出以及所有后续不以 a 开头和以 a[结尾的单词]

    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 等,你需要自己做一个,但这应该给你一个很好的起点。

于 2012-08-24T04:48:34.490 回答