python判断字符串包含中文、数字、英文

1.判断字符串只包含中文:

#encoding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

def check_contain_chinese(check_str):
    flag = True
    for ch in check_str.decode('utf-8'):
        if u'\u4e00' >= ch or ch >= u'\u9fff':
            flag =  False
    return flag

if __name__ == "__main__":
    print check_contain_chinese('中国')
    print check_contain_chinese(',。')
    print check_contain_chinese('中国123')

结果:

True
False
False

2.正则判断中文

zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
match = zhPattern.search(key)
    if match:
       print "是中文"




3. 正则判断是否含有英文和数字

import re
print(re.findall('.*r(.*)b.*', 'www.runoob.com'))

判断有数字:re.match(r'[+-]?\d+$', s) s 为数字, 返回数字位置 ,
not re.match(r'[+-]?\d+$', s) 返回为True说明不含有数字
判断有英文字符: re.match(r'[a-z]+',s) 返回小写字母位置
re.match(r'[a-z]+',s,re.I) 对大小写敏感。返回字母位置
not re.match(r'[a-z]+',s,re.I) 返回为True说明没有英文字符

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值