小编Kat*_*öhm的帖子

如何在Python中使用正则表达式获取前导零的所有索引

在 Python 中使用正则表达式(库 re(仅)),我想创建一个函数,该函数给出字符串中所有前导 0 的位置。

例如,如果字符串是:我的房子有 01 个花园和 003 个房间。我希望函数返回 13、27 和 28。

我尝试过例如:

import re
string = "My house has 01 garden and 003 rooms."
pattern = "(0+)[1-9]\d*"

print(re.findall(pattern,string))
Run Code Online (Sandbox Code Playgroud)

显然,输出给了我匹配但没有位置......

python regex python-re

3
推荐指数
1
解决办法
86
查看次数

标签 统计

python ×1

python-re ×1

regex ×1