判断字符串中是否包括中文数字字母及其他

'''
1、中文字符的编码范围是: u'\u4e00' -  u'\u9fa5';
2、但是跟python3的isalpha()函数(检测字符串中是否只有字符组成)一起判断时,要注意,isalpha()方法判断汉字时也返回True;

'''
import importlib
import sys
import re

importlib.reload(sys)


def check_contain_chinese(check_str):
    china_num = 0
    str_num = 0
    digit_num = 0
    other_num = 0
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fa5':
            china_num = china_num + 1
        elif ch.isalpha():
            str_num = str_num + 1
        elif ch.isdigit():
            digit_num = digit_num + 1
        else:
            other_num = other_num + 1
    print("汉字数量:", china_num)
    print("字母数量:", str_num)
    print("数字数量:", digit_num)
    print("其他非数字字母汉字的数量:", other_num)


# 通过正则表达式来判断;
def check_re_chinese(check_str):
    result = re.compile(u'[\u4e00-\u9fa5]')
    num = 0
    other_num = 0
    for ch in check_str:
        if result.search(ch):
            num = num + 1
        else:
            other_num = other_num + 1

    print("通过re得到汉字数量:", num)
    print("通过re得到非汉字数量:", other_num)


if __name__ == '__main__':
    check_contain_chinese("中12345qw!@#$%^&*erty 的是7我udf89 j50国")
    print("********************************")
    check_re_chinese("中12345qw!@#$ %^&*erty的是7我ud f89j50国")

运行结果:
汉字数量: 5
字母数量: 10
数字数量: 10
其他非数字字母汉字的数量: 10
********************************
通过re得到汉字数量: 5
通过re得到非汉字数量: 30

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值