简体   繁体   English

PHP正则表达式匹配所有大写字母中的单词或单词组

[英]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 . “HAPPY BIRTHDAY TOMMY”不会匹配并返回[0] -> HAPPY, [1] -> BIRTHDAY, [2] -> TOMMY ,但是整个组,例如[0] -> HAPPY BIRTHDAY TOMMY

The code I'm using below matches "HAPPY BIRTHDAY" together, or just "TOMMY", but not everything together. 我在下面使用的代码一起匹配“HAPPY BIRTHDAY”,或者只是“TOMMY”,但不是所有的东西。

[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?)+/ . 如果我理解正确,这应该是诀窍/([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'). 这应该抓住像HAPPY BIRTHDAY TOMMY这样的序列作为一套和生日快乐的tommy BOY作为两组('HAPPY BIRTHDAY'和'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" } 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 在Regexr上看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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