user3249398
user3249398

Reputation: 23

Regex brackets and brackets next to eachother

I'm a regex novice and I cannot figure out how to match the following:

String example:

"This is my string [something: something] and the string is very pretty [something: something][a][b][c]."

At the moment I got a regex that matches all start and end square brackets. \[([^]]*)\].

This yields the following

I want to group standalone brackets and brackets which has brackets next to it.

The regex should group it like;

Anyone able to help?

Upvotes: 0

Views: 92

Answers (1)

heemayl
heemayl

Reputation: 42127

You can do:

((?:\[[^]]*\])+)
  • The non-captured group (?:\[[^]]*\]) matches [, then any number of characters upto ] and then ]

  • The captured group ((?:\[[^]]*\])+) matches one or more occurrence of the non-captured group

Demo

Upvotes: 2

Related Questions