Regex to match uppercase Expressions and Words

故事扮演 提交于 2021-01-28 04:48:16

问题


Using Sublime Text 3 I want to extract only uppercase words and expressions from a text.

Example: Hello world! It's a SUNNY DAY for all.

If I use the find tool, I can extract all uppercase words separately by using this regex:

\b[A-Z]+\b

The results are SUNNY and DAY, but I would like to consider SUNNY DAY as a whole to extract trough the find tool, without leaving behind simple words like in:

It's SUNNY today.

回答1:


You can simply use

\b[A-Z]+(?:\s+[A-Z]+)*\b

See regex demo

I added (?:\s+[A-Z]+)* to the regex to match 0 or more sequences of:

  • \s+ - 1 or more whitespace
  • [A-Z]+ - 1 or more characters from A-Z range.

Note that in case you need to match Unicode uppercase letters, use \p{Lu} instead of [A-Z] (it will also match accented letters):

\b\p{Lu}+(?:\s+\p{Lu}+)*\b


来源:https://stackoverflow.com/questions/34066157/regex-to-match-uppercase-expressions-and-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!