重复正则表达式的一部分? [英] repeating a section of a regular expression?

查看:32
本文介绍了重复正则表达式的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解析电子表格的文本转储.我有一个正则表达式可以正确解析每一行数据,但它很长.它基本上只是匹配某个模式 12 或 13 次.

I'm having to parse a text dump of a spreadsheet. I have a regular expression that correctly parses each line of the data, but it's rather long. It's basically just matching a certain pattern 12 or 13 times.

我要重复的模式是

\s+(\w*\.*\w*);

这是正则表达式(缩写)

This is the regular expression (shortened)

^\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*);

有没有办法在不复制粘贴的情况下匹配一定次数的模式?这些部分中的每一个都对应于我需要的所有数据列.顺便说一下,我正在使用 Python.谢谢!

Is there a way to match a pattern a set number of times without copy pasting like this? Each of those sections correspond to data columns, all of which I need. I'm using Python by the way. Thanks!

推荐答案

如何使用:

[x.group() for x in re.finditer(r'(\s+(\w*\.*\w*);)*', text)]

你找到 findall 方法了吗?或者考虑在 ; 处拆分?

Did you find the findall method yet? Or consider splitting at ;?

map(lambda x: x.strip(), s.split(";"))

可能是您真正想要的.

这篇关于重复正则表达式的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆