在 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)
显然,输出给了我匹配但没有位置......