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说明没有英文字符