Reputation: 2989
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
(A) return only the string inside each []
(brackets included). I've solved this with \[(.*?)\]
(B) return everything except what is inside []
(brackets excluded from the end result). So the opposite of the previous one.
Expected result:
["[I]", "[don't]", "[want to]", "[be captured]"]
"I want CaptUre this number 9 and this Word"
How can I do it?
Upvotes: 0
Views: 148
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'
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.
It's all simple - search for anything in brackets and Join
them.
Upvotes: 1
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
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 sed can do it:
$ sed -E 's/\[[^]]+\]//g' file
I want cAptUre this number 9 and this Word
Upvotes: 0