正则表达式匹配大写表达式和单词 [英] Regex to match uppercase Expressions and Words

查看:78
本文介绍了正则表达式匹配大写表达式和单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Sublime Text 3 我只想从文本中提取大写的单词和表达式.

示例:你好,世界!对所有人来说,这是一个晴天.

如果我使用查找工具,我可以使用这个正则表达式分别提取所有大写单词:

\b[A-Z]+\b

结果是 SUNNY 和 DAY,但我想将 SUNNY DAY 视为一个整体来通过查找工具进行提取,而不会留下诸如 in 之类的简单词:

今天阳光明媚.

解决方案

你可以简单地使用

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

参见

请注意,如果您需要匹配 Unicode 大写字母,请使用 \p{Lu} 而不是 [AZ](它也会匹配重音字母):

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

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.

解决方案

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

这篇关于正则表达式匹配大写表达式和单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆