Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex match word or group of words in all capital letters

Tags:

regex

php

How can I use regex to match a word or groups of words that are in all capital letters?

I believe I've solved the problem half way, although it may not be the right way.

I am trying to catch one word in all caps, or two or three - basically if they're in succession I want them captured as a group, not as each word itself.

e.g.:
"HAPPY BIRTHDAY TOMMY" wouldn't match and return [0] -> HAPPY, [1] -> BIRTHDAY, [2] -> TOMMY, but the whole group, such as [0] -> HAPPY BIRTHDAY TOMMY.

The code I'm using below matches "HAPPY BIRTHDAY" together, or just "TOMMY", but not everything together.

[A-Z]{1,}\s[A-Z]{1,}|\b[A-Z]{1,}\b
like image 833
Ryan Cooper Avatar asked Feb 22 '23 11:02

Ryan Cooper


1 Answers

You can use the regex:

(?=[A-Z])([A-Z\s]+)

See it

like image 185
codaddict Avatar answered Mar 02 '23 22:03

codaddict