1

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.

Andrej
  • 13
  • 3
  • 1
    Could you please format your question so it's more readable? – rowana Jul 05 '17 at 14:50
  • 2
    It doesn't match other digits since you are matching everything `.*` after first match. You are after something like a positive lookahead [**`\d+(?=[^][]*\])`**](https://regex101.com/r/9LVhB9/1/) – revo Jul 05 '17 at 14:53
  • Example above you have open bracket what it is happening if you have [ bla bla 123,456, bla [abg,789] bla it should return 123,456, 789 or just 789 ? – Ezequiel De Simone Jul 05 '17 at 15:00
  • problem solved thank you very much, i just miss typed your regex ;) – Andrej Jul 05 '17 at 15:09

1 Answers1

1

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

Graham
  • 7,431
  • 18
  • 59
  • 84
revo
  • 47,783
  • 14
  • 74
  • 117