循环遍历python正则表达式匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12870178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looping through python regex matches
提问by da5id
This has to be easier than what I am running into. My problem is turning a string that looks like this:
这必须比我遇到的更容易。我的问题是转动一个看起来像这样的字符串:
ABC12DEF3G56HIJ7
into
进入
12 * ABC
3 * DEF
56 * G
7 * HIJ
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.
而且我一生都无法使用 REGEX 匹配设计一组正确的循环。问题的关键是代码必须是完全通用的,因为我不能假设这些[A-Z]片段会有多长,也不能假设这些片段会有多长[0-9]。
Thank you for any assistance!
感谢您的任何帮助!
采纳答案by Ray Toal
回答by Mithril
It is better to use re.finditerif you dataset is large:
如果数据集很大,最好使用re.finditer:
import re
s = "ABC12DEF3G56HIJ7"
pattern = re.compile(r'([A-Z]+)([0-9]+)')
for m in re.finditer(pattern, s):
print m.group(2), '*', m.group(1)

