简体   繁体   中英

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

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.

eg:
"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

You can use the regex:

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

See it

I'm sure I fully understand what you need, but in order to group you have to use parenthesis (). try this:

([A-Z]+)\b([A-Z]+)\b([A-Z]+)\b

This should capture three consecutive all-caps words.

If I understand you correctly, this should do the trick /([AZ]\\s?)+/ . This should catch sequences like HAPPY BIRTHDAY TOMMY as a set and HAPPY BIRTHDAY tommy BOY as two sets ('HAPPY BIRTHDAY' and 'BOY').

What about this

$str = "My test sentence HAPPY BIRTHDAY TOMMY this is lower case an UPPERCASE more lowercase";
if (preg_match_all('/\\b(?=[A-Z])[A-Z ]+(?=\\W)/',$str,$match)) {                      
    var_dump($match[0]);
}

result is

array(2) { [0]=> string(20) "HAPPY BIRTHDAY TOMMY" 1 => string(9) "UPPERCASE" }

The usage of the lookahead at the end ensures that there is no whitespace included at the end, as it would happen if a word boundary is used and there is another word following.

See it here on Regexr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM