Home > database >  Remove fixed string only when there is only the fixed string
Remove fixed string only when there is only the fixed string

Time:05-31

I have a couple of strings:

Green color
Red color
 color
Pink color

Now I want to remove " color" but only when that string is found. So I want to end up with the following sequence:

Green color
Red color
Pink color

I tried some basic regexp but I am starting to think this is not possible, or am I wrong?

CodePudding user response:

This will match color preceded by a space that is not preceded by a word boundary.

\B color

https://regex101.com/r/Jxz7d7/1

CodePudding user response:

My code on a quick hand:

System.Collections.Generic.List<string> lines = str.Split(Environment.NewLine).ToList();
      for(int i = 0; i < lines.Count; i  )
      {
            if(lines[i].Trim() == "color")
            {
                lines.Remove(lines[i]);
                i--;
            }
      }
      System.Console.WriteLine(string.Join(Environment.NewLine, lines));

If that's string is certainly getting from file, then I have other solution

  • Related