遍历python正则表达式匹配 [英] Looping through python regex matches
本文介绍了遍历python正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这必须比我遇到的要容易.我的问题是变成一个看起来像这样的字符串:
This has to be easier than what I am running into. My problem is turning a string that looks like this:
ABC12DEF3G56HIJ7
进入
12 * ABC
3 * DEF
56 * G
7 * HIJ
在我的一生中,我无法使用REGEX匹配设计正确的循环集.问题的症结在于代码必须是完全通用的,因为我无法假设[A-Z]
片段将是多长时间,也无法确定[0-9]
片段将有多长时间.
And I can't, for the life of me, design a correct set of loops using REGEX matching. The crux of the issue is that the code has to be completely general because I cannot assume how long the [A-Z]
fragments will be, nor how long the [0-9]
fragments will be.
谢谢您的帮助!
推荐答案
Python的re.findall
应该适合您.
Python's re.findall
should work for you.
import re
s = "ABC12DEF3G56HIJ7"
pattern = re.compile(r'([A-Z]+)([0-9]+)')
for (letters, numbers) in re.findall(pattern, s):
print(numbers, '*', letters)
这篇关于遍历python正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文