【发布时间】:2018-03-01 16:43:28
【问题描述】:
我有一个字符串如下:theatre = 'Regal Crown Center Stadium 14'
我想根据每个单词的第一个字母将其分解为首字母缩写词,但同时包含两个数字:
所需的输出 = 'RCCS14'
我的代码尝试如下:acronym = "".join(word[0] for word in theatre.lower().split())
acronym = "".join(word[0].lower() for word in re.findall("(\w+)", theatre))
acronym = "".join(word[0].lower() for word in re.findall("(\w+ | \d{1,2})", theatre))
acronym = re.search(r"\b(\w+ | \d{1,2})", theatre)
我最终得到类似:rccs1 但似乎无法捕捉到最后一个数字。在某些情况下,数字也位于名称的中间:'Regal Crown Center 14 Stadium' 也是如此。蒂亚!
【问题讨论】:
-
是否有可能连续出现多个大写字母,例如
AMC Loews Uptown 1?期望的输出是什么?IMAX也是如此
标签: python regex elements capture