如何使用重新匹配找到数字?

2024-09-22 20:32:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用pythonre模块:

import re

res = re.match(r"\d+", 'editUserProfile!input.jspa?userId=2089')
print(res)

我得到了res的None类型,但是如果我将match替换为findall,我可以找到2089。在

你知道问题出在哪里吗?在


Tags: 模块importrenone类型inputmatchres
1条回答
网友
1楼 · 发布于 2024-09-22 20:32:36

问题是您使用^{}在字符串中搜索子字符串。在

方法match()只对整个字符串有效。如果要在字符串中搜索子字符串,应使用^{}。在

正如评论中的khelwood所述,您应该看看:Search vs Match。在


代码:

import re
res = re.search(r"\d+", 'editUserProfile!input.jspa?userId=2089')
print(res.group(0))

输出:

^{pr2}$

或者,您可以使用.split()来隔离用户id

代码:

s = 'editUserProfile!input.jspa?userId=2089'
print(s.split('=')[1])

输出:

^{pr2}$

相关问题 更多 >