Python中文
首页
教程
问答
标签
搜索
登录
注册
Python regex查找nth numb
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>为了学习的目的,我做了一些来自<em>代码斗争</em>的regex挑战。现在他们有非常具体的规则来改变什么才能得到正确的结果。我用一种不同的方法解决了这个难题,我不知道他们要我怎么做。在</p> <p><strong>Q:</strong>返回不以零开头的给定字符串的第n个数字</p> <p><strong>示例</strong></p> <pre><code># I can only change ... to solve it # def nthNumber_asked(s, n): # pattern = ... # return re.match(pattern, s).group(1) # How I solved it def nthNumber_mine(s, n): return re.findall(r'(0*)([0-9]+)', s)[n-1][1] s1 = "8one 003number 201numbers li-000233le number444" s2 = "LaS003920tP3rEt4t04Yte0023s3t" print(nthNumber_mine(s1, 4)) # Expected outcome = 233 print(nthNumber_mine(s2, 4)) # Expected outcome = 4 </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<pre><code>def nthNumber_mine(s, n): return re.search(r'(?:\d+.+?){%d}0*(\d+)' % (n-1), s).group(1) </code></pre> <p><code>(?:\d+\D+?){%d}</code>匹配至少1个数字后跟至少0个其他字符,持续n-1次。然后至少匹配0个零,然后至少匹配一个数字(这就是您要查找的)。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何添加虚拟方法
4 回答
如何添加表示整数的擦边字符串?
5 回答
如何添加要在Bokeh中使用的新font.ttf文件?
9 回答
如何添加要显示的矩阵XY轴编号和XY轴
4 回答
如何添加计数?
10 回答
如何添加计数器函数?
6 回答
如何添加计数器列来计算数据帧中另一列中的特定值?
5 回答
如何添加计数器来跟踪while循环中的月份和年份?
2 回答
如何添加计数并删除countplot的顶部和右侧脊椎?
6 回答
如何添加计时器wx.应用程序更新窗口对象的主循环?
5 回答
如何添加评论到帖子?PostDetailVew,Django 2.1.5
7 回答
如何添加评论拉梅尔亚姆
5 回答
如何添加诸如矩阵Python/Pandas之类的数据帧?
4 回答
如何添加谷歌地点自动完成到Flask?
2 回答
如何添加超时、python discord bot
2 回答
如何添加超过1dp的检查
5 回答
如何添加距离方法
8 回答
如何添加跟随游戏的敌人精灵
9 回答
如何添加路径以便python可以找到程序?
1 回答
如何添加身份验证/安全性以使用happybase访问HBase?
7 回答