问题
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 fromA-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