1
长度为2的所有字符串我想提取文本的所有两字母串用正则表达式,例如:提取与Re.Findall
just a test
会得到我ju, us, st, te, es, st
我尝试使用:re.findall(r'\w{2}',text)
,但它只分为2个字母的字符串,并给我ju, st, te, st
非常感谢您的帮助。
长度为2的所有字符串我想提取文本的所有两字母串用正则表达式,例如:提取与Re.Findall
just a test
会得到我ju, us, st, te, es, st
我尝试使用:re.findall(r'\w{2}',text)
,但它只分为2个字母的字符串,并给我ju, st, te, st
非常感谢您的帮助。
我将离开正则表达式的解决方案,正则表达式的专家(这我不是),因为它可以在没有正则表达式很简单,在一个班轮列表解析完成:
s = "just a test"
result = ["".join(x) for w in s.split() if len(w)>1 for x in zip(w,w[1:])]
print(result)
结果:
['ju', 'us', 'st', 'te', 'es', 'st']
刚刚拆分的话,用了不到2个字符过滤掉的话,并使用zip
只能如果T交错他们对他们的移动副本当然这里没有标点符号。
使用['re.findall(r'(?=(\ w {2}))',text)'](http://ideone.com/XucVPm)。 –
没有正则表达式:'print([“”。join(x)for w in“just a test”.split()if len(w)> 1 for zip in(w,w [1:])]))' –
@sin:他标记为重复,然后评论(未回答)以帮助OP在他的具体问题。我只是希望我可以发布我的非正则表达式解决方案。 –