You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Regular Expressions提取仅含一个大写字母且无数字的单词?

提取仅含单个大写字母的纯字母单词的正则方案

核心正则表达式

\b(?![a-zA-Z]*[A-Z]{2,})(?![a-zA-Z]*[^a-zA-Z])[a-zA-Z]*[A-Z][a-zA-Z]*\b

正则各部分解释

  • \b:单词边界,确保匹配完整单词,避免截取单词片段
  • (?![a-zA-Z]*[A-Z]{2,}):负向前瞻断言,排除包含2个及以上大写字母的单词
  • (?![a-zA-Z]*[^a-zA-Z]):负向前瞻断言,排除包含数字、下划线或其他非字母字符的单词
  • [a-zA-Z]*[A-Z][a-zA-Z]*:匹配包含且仅包含一个大写字母的纯字母序列,大写字母可出现在单词任意位置
  • \b:结尾单词边界,锁定完整匹配范围

代码示例

JavaScript 实现

const sentence = "My favorite words are appLe, warDrobe and hUsh — sf_dsfsdF, 331ffsF and Leopard1997 don't qualify";
const regex = /\b(?![a-zA-Z]*[A-Z]{2,})(?![a-zA-Z]*[^a-zA-Z])[a-zA-Z]*[A-Z][a-zA-Z]*\b/g;
const validWords = sentence.match(regex);
console.log(validWords); // 输出: ["appLe", "warDrobe", "hUsh"]

Python 实现

import re

sentence = "My favorite words are appLe, warDrobe and hUsh — sf_dsfsdF, 331ffsF and Leopard1997 don't qualify"
regex = r'\b(?![a-zA-Z]*[A-Z]{2,})(?![a-zA-Z]*[^a-zA-Z])[a-zA-Z]*[A-Z][a-zA-Z]*\b'
valid_words = re.findall(regex, sentence)
print(valid_words) # 输出: ['appLe', 'warDrobe', 'hUsh']

补充说明

  • 如果需要支持Unicode字母(如非英文的大小写字母),可将正则中的[a-zA-Z]替换为\p{L}(需确保正则引擎支持Unicode属性,比如Python的re.UNICODE模式、JavaScript的u标志)
  • 单词边界\b仅对字母/数字/下划线有效,但我们已经通过负向前瞻排除了下划线和数字,因此不会影响匹配准确性

内容的提问来源于stack exchange,提问作者Alan Anderson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:05:24