developer747
developer747

Reputation: 15958

Regex get text inside one pair of square brackets

In the text below how do I get the text inside the first pair of square brackets

xxxx [I can be any text and even have digits like 0 25 ] [sdfsfsf] [ssf sf565wf]

This is what I tried. But it goes till the last square bracket.

.*\[.*]

What i want selected is

[I can be any text and even have digits like 0 25 ]

Upvotes: 2

Views: 1201

Answers (3)

Quinn
Quinn

Reputation: 4504

Another one with DEMO. A bit complicated though:

(\[[^\]]+\])[^\[\]]*(?:\[[^\]].*\])

EXPLANATION

(\[[^\]]+\])            #capturing group
                        #match first [] pair

[^\[\]]*                #match characters except ] and [

(?:\[[^\]].*\])         #non-capturing group
                        #match all the rest [] pairs
                        #this is a greedy match

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

If you don't want to go past the closing square bracket, use [^\]]* in place of .*:

^[^\[]*(\[[^\]]*])

Add ^ anchor at the beginning if you would like to search multiple lines.

Add a capturing group around the square brackets, and get the content of that group to obtain the text that you need.

Demo.

Upvotes: 2

kyle
kyle

Reputation: 442

* and + are 'greedy' by default, so they try to match as much text as possible. If you want them to match as little as possible, you can make them non-greedy with ?, eg .*\[.*?\]. Also, the .* at the beginning matches any number of any characters before the opening square bracket, so this regex will match all text up to a ']' as long as there is a '[' somewhere before the ']'. If you only want to match the brackets and their contents, you want simply \[.*?\].

Non-greedy modifiers with ? are not supported in all regex engines; if it's available to you you should write it with ? because it makes your intent clearer, but if you are using a simpler regex engine you can achieve the same effect by using \[[^\]]*\] instead. This is a negated character class, which matches as many as possible of any character except ']'.

Upvotes: 0

Related Questions