Andrej
Andrej

Reputation: 13

Find all numbers within square brackets using REGEX

I searched some posts but can't find the right solution for my problem.

I want all numbers that are within [].

I tested the following regex on regex101:

\\[.\*([0-9]++).*\\] .....    /gU <- Flags

Test string:

Test bla bla [us_image_slider ids="6207,6204,6203,6199,6484,6470" nav="thumbs" img_size="us_img_size_2"] some numbers 232323 [img="344"] and [daas 23344 2333]2323 ( hello 233 ) 

My Regex finds only the first number and not the others within the square brackets :(

Result has to be : match0 = 6207 match1 = 6204 ...

Hope you can help me, thanks.

Upvotes: 1

Views: 145

Answers (1)

revo
revo

Reputation: 48751

It doesn't match other digits since you are matching everything .* after first match.

You are looking for something like a positive lookahead:

\d+(?=[^][]*\])

Live demo

Upvotes: 1

Related Questions