将匹配项从正则表达式合并为单个列表
我正在尝试将骆驼中的字符串分离为单个列表,
我设法将单词用正则表达式分开
,但是我对如何创建我尝试串联列表的所有匹配的单个列表毫无意义
,但我不喜欢这样``认为它在我的情况下会起作用
n="SafaNeelHelloAByeSafaJasleen"
patt=re.compile(r'([A-Z][a-z]*|[a-z$])')
matches=patt.finditer(n)
for match in matches:
a=match.group()
list=a.split()
print(list)
:
['Safa']
['Neel']
['Hello']
['A']
['Bye']
['Safa']
['Jasleen']
所需的输出:
['Safa','Neel','Hello','A','Bye','Safa','Jasleen']
I am trying to separate a string in CamelCase into a single list
I managed to separate the words with regular expressions
But I am clueless on how create a single list of all the matches
I tried to concatenate the lists, append something like that but I don't think it would work in my case
n="SafaNeelHelloAByeSafaJasleen"
patt=re.compile(r'([A-Z][a-z]*|[a-z$])')
matches=patt.finditer(n)
for match in matches:
a=match.group()
list=a.split()
print(list)
output:
['Safa']
['Neel']
['Hello']
['A']
['Bye']
['Safa']
['Jasleen']
Desired output:
['Safa','Neel','Hello','A','Bye','Safa','Jasleen']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找
re.findall()
,而不是re.finditer()
:You're looking for
re.findall()
, notre.finditer()
:您可以将匹配项附加到新列表:
new_list的输出:
You can append the matches to new list:
Output of new_list: