gpt4 book ai didi

python - python 中的正则表达式。输出不正确

转载 作者:行者123 更新时间:2023-12-01 04:38:00 25 4
gpt4 key购买 nike

我想检查字符串是否以数字开头,然后包含 9 个字母数字字符。但是,这会返回 yes 而不是 no。为什么?

import re

if re.search(r'[0-9][0-9]{9}','92211sssff222'):
print "yes"
else:
print "no"

最佳答案

应该是这样的。既然你说它应该开始,你就必须使用大写符号^来指定开始

根据您的正则表达式,您将匹配十个连续数字,这些数字可能出现在字符串中的任何位置

import re

if re.search(r'^[0-9][a-zA-Z0-9]{9}','9sssssssff222'):
print ("yes")
else:
print ("no")

yes

关于python - python 中的正则表达式。输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31391064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com