2024-09-22 20:32:36 发布
网友
我尝试使用pythonre模块:
re
import re res = re.match(r"\d+", 'editUserProfile!input.jspa?userId=2089') print(res)
我得到了res的None类型,但是如果我将match替换为findall,我可以找到2089。在
None
match
findall
2089
你知道问题出在哪里吗?在
问题是您使用^{}在字符串中搜索子字符串。在
方法match()只对整个字符串有效。如果要在字符串中搜索子字符串,应使用^{}。在
match()
正如评论中的khelwood所述,您应该看看:Search vs Match。在
代码:
import re res = re.search(r"\d+", 'editUserProfile!input.jspa?userId=2089') print(res.group(0))
输出:
或者,您可以使用.split()来隔离用户id
.split()
s = 'editUserProfile!input.jspa?userId=2089' print(s.split('=')[1])
问题是您使用^{} 在字符串中搜索子字符串。在
方法} 。在
match()
只对整个字符串有效。如果要在字符串中搜索子字符串,应使用^{正如评论中的khelwood所述,您应该看看:Search vs Match。在
代码:
输出:
^{pr2}$或者,您可以使用
.split()
来隔离用户id代码:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐