鱼C论坛

 找回密码
 立即注册
查看: 2899|回复: 2

[已解决]正则表达求助

[复制链接]
发表于 2022-12-15 19:07:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1671102394012.jpg 为什么这个程序没有屏蔽掉没有¥#@的2W开头的字符串呢
程序为:
import re
a=input("input")
strlist=a.split(",")
for word in strlist:
    if len(word)>=6 and len(word)<=12:
        if re.search("[a-z]",word) != "":
            if re.search("[A-Z]",word) != "":
                if re.search("[0-9]",word) != "":
                    if re.search("[$#@]",word) != "":
                        print(word)
最佳答案
2022-12-15 20:34:56
本帖最后由 lxping 于 2022-12-15 20:37 编辑

正则表达式 re.search() 匹配不成功的时候返回 NoneType
word = "2We3345"
a = re.search("[$#@]",word)
a
a != ""
True
type(a)
<class 'NoneType'>
type("")
<class 'str'>
改一下
import re
a=input("input")
strlist=a.split(",")
for word in strlist:
    if len(word)>=6 and len(word)<=12:
        if re.search("[a-z]",word):
            if re.search("[A-Z]",word):
                if re.search("[0-9]",word):
                    if re.search("[$#@]",word):
                        print(word)
运行结果
inputABd1234@1,a F#,2w3E*,We3345
ABd1234@1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-15 20:34:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 lxping 于 2022-12-15 20:37 编辑

正则表达式 re.search() 匹配不成功的时候返回 NoneType
word = "2We3345"
a = re.search("[$#@]",word)
a
a != ""
True
type(a)
<class 'NoneType'>
type("")
<class 'str'>
改一下
import re
a=input("input")
strlist=a.split(",")
for word in strlist:
    if len(word)>=6 and len(word)<=12:
        if re.search("[a-z]",word):
            if re.search("[A-Z]",word):
                if re.search("[0-9]",word):
                    if re.search("[$#@]",word):
                        print(word)
运行结果
inputABd1234@1,a F#,2w3E*,We3345
ABd1234@1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-16 23:17:38 | 显示全部楼层
懂了 谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 07:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表