python regex repetition with capture question

╄→гoц情女王★ 提交于 2019-12-02 01:27:03

Most or all regular expression engines in common use, including in particular those based on the PCRE syntax (like Python's), label their capturing groups according to the numerical index of the opening parenthesis, as the regex is written. So no, you cannot use capturing groups alone to extract an arbitrary, variable number of subsequences from a string.

The closest you can get (as far as I know) is to manually write out a certain number of capturing groups, something like this:

s = ...
res = re.match(r'\D*' + 25 * r'(\d+)\D+')
numbers = [r for r in res.groups() if r is not None]

This will get you up to 25 groups of digits. If you need more, replace 25 with some higher number.

I wouldn't be surprised if this were less efficient than the iterative approach with findall(), although I haven't tested it.

This will match all the numbers before the dot:

s = "zzz98zzz67zzz89zzz45vdvd55lplp111.mp3"
res = re.findall("[0-9]+(?=.*\\.)", s)
print(res)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!