0

I am trying to use regex to get rid of strings between specific characters, for example "[" and "]":

input = "There is a [blue] cloud on top of that [big] elephant";
desiredOut = "There is a cloud on top of that elephant"; // this is what i want

But if I use regex to replace what is between "[" and "]" I remove everything between the first and last of this characters:

string input = "There is a [blue] cloud on top of that [big] elephant";
string regex = "(\\[.*\\])" ;
string actualOut = Regex.Replace(input, regex, "");

actualOut = "There is a elephant" // this is what i get

Any clues on how to remove the stuff between inmediate delimiters? thanks.

Santi
  • 51
  • 1
  • 5

4 Answers4

1

This slight modification should solve your problem:

string regex = "\\[(.*?)\\]";

This bit (.*?) will match everything but will take as little as possible.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
0
var input = "There is a (blue) cloud on top of that(big) elephant";
var output = Regex.Replace(input, @" ?\(.*?\)", string.Empty);

this will do the job

0

The reason your regex removes everything between the first [ and last ] is because by default modifiers are greedy, ie. they match as many letters as possible.

You can use the lazy match as in the other answers, or you can use

string regex = @"\[[^\]]*\]"

This regex matches a left square bracket, then it takes anything except a right square bracket, and then finishes at the right square bracket.

Destrictor
  • 752
  • 1
  • 4
  • 15
0

To remove also the leading or trailing space, you could use this

string actualOut = Regex.Replace(input, @"^\[[^\]]*\]\s+|\s+\[[^\]]*\]", ""); 

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274