Leonardo Lurci
Leonardo Lurci

Reputation: 2989

Regex matching everything except what is between [ ... ]

I'm looking for two Regex which given a string like the following one:

I want [I] cAptUre this [don't] number [want to] 9 and [be captured] this Word

Expected result:

How can I do it?

Upvotes: 0

Views: 148

Answers (3)

JohnyL
JohnyL

Reputation: 7122

I have altered source string a bit to include edge cases:

[We] I want [I] cAptUre this [don't] number [want to] 9 and [be captured] this Word [89]

var without_brackets = Regex.Replace(s, @"\s*\[.*?\]\s*", " ").Trim();
var in_brackets = string.Join(" ", Regex.Matches(s, @"\[(.*?)\]")
                        .OfType<Match>()
                        .Select(m => m.Groups[1].Value));

WriteLine($"Without brackets: '{without_brackets}'");
WriteLine($"In brackets: '{in_brackets}'");

// Output:
// Without brackets: 'I want cAptUre this number 9 and this Word'
// In brackets:      'We I don't want to be captured 89'

Without brackets

The pattern searches for the brackets and all spaces to both sides - and replaces them with a space. I used * quantifier for \s in order to include edge cases when there can be words in brackets at the end and at the start of the string. Also, Trim is required to remove extra spaces for edge cases.

With brackets

It's all simple - search for anything in brackets and Join them.

Upvotes: 1

JimiLoe
JimiLoe

Reputation: 978

Concerning (A), you can use a Match.Group and the pattern @"(\[[^]]+\])" suggested by Gilles Quenot. Concerning (B), you can use Regex.Replace to replace your text brackets:

var input = "I want[I]  cAptUre this[don't] number [want to] 9 and [be captured] this Word";
var pattern = @"(\[[^]]+\])";

var result = Regex.Replace(input, pattern, "");
Console.WriteLine(result);

The output is

I want  cAptUre this number  9 and  this Word

If you want to avoid duplicate white spaces in the output you can replace them too:

result = Regex.Replace(result, @"(\s\s+)", " ");

Upvotes: 1

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 184955

Like this for the first requirement :

(\[[^]]+\])

Check https://regex101.com/r/NdmSRF/1

For the second requirement, not sure a regex alone can do this, but can do it:

$ sed -E 's/\[[^]]+\]//g' file
I want   cAptUre this  number  9 and  this Word

Upvotes: 0

Related Questions