Python如何在字符串中查找第二个大写字母的索引

时间:2015-03-17 14:37:19

标签: regex python-2.7

我想获取任何字符串中第二个大写字母的索引。

例如,在字符串"保罗喜欢冰淇淋"我想获得" I"的索引,它是11.它应该适用于任何字符串。

2 个答案:

答案 0 :(得分:0)

您可以使用:

m = re.search(r'^([^A-Z]*[A-Z]){2}', 'Paul likes Ice cream.');
print m.span()[1]
12

答案 1 :(得分:0)

这样的东西
>>> string = " Paul likes Ice cream." 
>>> [ match.start() for match in re.finditer ("[A-Z]", string) ][1]
12