【发布时间】:2022-11-03 01:02:55
【问题描述】:
在 Python 中使用 Regex(库 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))
显然,输出给了我匹配但没有位置......
【问题讨论】:
-
这回答了你的问题了吗? Find the indexes of all regex matches?