Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex (php) to extract only numbers or only letters inside square brackets (excluding the brackets)

Tags:

regex

I have this regex that extracts anything inside braquets: [^[\]]+(?=])

1) I want to extract the content within the brackets as a whole if it contains letters only (a-z)

2) I want to extract the content within the brackets as a whole if it contains numbers only (0-9)

So, I need 2 regex.

like image 871
Sergelie Avatar asked Dec 02 '25 13:12

Sergelie


1 Answers

Try this

(?<=\[)[a-zA-Z]+(?=\])

Demo

(?<=\[)[0-9]+(?=\])

Demo

like image 85
Tim007 Avatar answered Dec 05 '25 09:12

Tim007



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!