使用正则表达式查找字符串中的小写字母并附加到列表。python

使用正则表达式查找字符串中的小写字母并附加到列表。python,python,regex,string,lowercase,findall,Python,Regex,String,Lowercase,Findall,我正在寻找一种从同时包含大写字母和可能包含小写字母的字符串中获取小写值的方法 这里有一个例子 sequences = ['CABCABCABdefgdefgdefgCABCAB','FEGFEGFEGwowhelloFEGFEGonemoreFEG','NONEARELOWERCASE'] #sequences with uppercase and potentially lowercase letters 这就是我想要输出的内容 upper_output = ['CABCABCABCABCA

我正在寻找一种从同时包含大写字母和可能包含小写字母的字符串中获取小写值的方法

这里有一个例子

sequences = ['CABCABCABdefgdefgdefgCABCAB','FEGFEGFEGwowhelloFEGFEGonemoreFEG','NONEARELOWERCASE'] #sequences with uppercase and potentially lowercase letters
这就是我想要输出的内容

upper_output = ['CABCABCABCABCAB','FEGFEGFEGFEGFEGFEG','NONEARELOWERCASE'] #the upper case letters joined together
lower_output = [['defgdefgdefg'],['wowhello','onemore'],[]] #the lower case letters in lists within lists
lower_indx = [[9],[9,23],[]] #where the lower case values occur in the original sequence
所以我希望下面的输出列表是一个子列表列表。子列表将包含所有小写字母字符串

我在考虑使用正则表达式

import re

lower_indx = []

for seq in sequences:
    lower_indx.append(re.findall("[a-z]", seq).start())

print lower_indx
对于我尝试的小写列表:

lower_output = []

for seq in sequences:
    temp = ''
    temp = re.findall("[a-z]", seq)
    lower_output.append(temp)

print lower_output
但是这些值不在单独的列表中(我仍然需要加入它们)


除了正则表达式,您还可以在此处使用
itertools.groupby

In [39]: sequences = ['CABCABCABdefgdefgdefgCABCAB','FEGFEGFEGwowhelloFEGFEGonemoreFEG','NONEARELOWERCASE'] #sequences with uppercase and potentially lowercase letters

In [40]: lis=[["".join(v) for k,v in groupby(x,key=lambda z:z.islower())] for x in sequences]

In [41]: upper_output=["".join(x[::2]) for x in lis]

In [42]: lower_output=[x[1::2] for x in lis]

In [43]: upper_output
Out[43]: ['CABCABCABCABCAB', 'FEGFEGFEGFEGFEGFEG', 'NONEARELOWERCASE']

In [44]: lower_output
Out[44]: [['defgdefgdefg'], ['wowhello', 'onemore'], []]

In [45]: lower_indx=[[sequences[i].index(y) for y in x] for i,x in enumerate(lower_output)]

In [46]: lower_indx
Out[46]: [[9], [9, 23], []]
听起来(我可能误解了你的问题)你只需要捕获一系列小写字母,而不是每个单独的小写字母。这很简单:只需将
+
量词添加到正则表达式中

for seq in sequences:
    lower_output.append(re.findall("[a-z]+", seq)) # add substrings
+
量词指定您想要前面表达式中的“至少一个,以及在一行中可以找到的数量”(在本例中为
'[a-z]'
)。因此,这将在一个组中捕获所有小写字母的完整运行,这将使它们在输出列表中按您所希望的方式显示

如果您想保留列表结构的列表并同时获取索引,那么它会变得有点丑陋,但它仍然非常简单:

for seq in sequences:
    matches = re.finditer("[a-z]+", seq) # List of Match objects.
    lower_output.append([match.group(0) for match in matches]) # add substrings
    lower_indx.append([match.start(0) for match in matches]) # add indices

print lower_output
>>> [['defgdefgdefg'], ['wowhello', 'onemore'], []]

print lower_indx
>>> [[9], [9, 23], []]

…那么你的问题到底是什么?你只需要知道如何用正则表达式一次捕获所有内容,还是需要加入字符?如果你想要一个列表,为什么不在非字母的运行中拆分,然后分解。尝试使用一个匹配实例进行重新搜索(r'[a-z]+',sequence[i]),我甚至不知道它的存在。知道如何获得索引值吗?@draconisthe0ry当然知道;我已经更新了我的答案,包括这些。
for seq in sequences:
    matches = re.finditer("[a-z]+", seq) # List of Match objects.
    lower_output.append([match.group(0) for match in matches]) # add substrings
    lower_indx.append([match.start(0) for match in matches]) # add indices

print lower_output
>>> [['defgdefgdefg'], ['wowhello', 'onemore'], []]

print lower_indx
>>> [[9], [9, 23], []]