regex

Regular expression to extract text between square brackets & last string out of the square bracket


I am new to the regex and trying to write the RegEx for the below string.

[1] [2] [3] [] [5[any]] my text

I want something like

1
2
3

5[any]
my text

I have tried the /\[(.*?)\]/g but it not working with inner brackets & the last word outside of the brackets

https://regex101.com/r/NYvxMC/2


Solution

  • Try:

    ((?<=\[)(?:[^\[\]]|\[[^\[\]]+\])*(?=\])|(?<=\s)[\w\h:.,]+$)
    

    See: regex101


    Explanation