# . # 点可代表一切字符
# \ # 起转义作用
# [...] # 指代方括号中的任意字符
# \d # 指代数字0-9
# \D # 指代非数字
# \s # 指代一切空格,包括tab制表符、空格、换行等
# \S # 指代非空格
# \w # 指代大小写字母、数字和下划线
# \W # 指代非大小写字母、数字和下划线
# * # 匹配前面字符 >=0 次
# + # 匹配前面字符1次及以上
# ? # 匹配前面字符0次或1次
# {m} # 匹配m次
# {m,n} # 匹配m到n次
# {m,} # 至少匹配m次
s1 = '''
name:sim ,gender:f, age:27,address:JiangSu,Edu:yjs
'''
keys = re.findall(r'(\w+):', s1 ,re.I)
# ['name', 'gender', 'age', 'address', 'Edu']
values = re.findall(r':(\w+)', s1, re.I)
# ['sim', 'f', '27', 'JiangSu', 'yjs']
key = re.findall(r'\w+:', s1 ,re.I)
# ['name:', 'gender:', 'age:', 'address:', 'Edu:']
value = re.findall(r':\w+', s1, re.I)
# [':sim', ':f', ':27', ':JiangSu', ':yjs']
print(key)
print(value)
s2 = 'oh,mygard,Python is an easy language, do you like it'
rule = re.compile(r'\w*a\w*')
print(re.findall(rule, s2))
# ['mygard', 'an', 'easy', 'language']
# s3 = 'oh,mygard,Python is an easy language, do you like it'
rule = re.compile(r'\w+a\w+')
print(re.findall(rule, s2))
# ['mygard', 'easy', 'language']
s4 = 'good morning Pairs, this picture is good!'
print(re.findall(r'p\w+', s4, re.I))
# ['Pairs', 'picture']
s = 'sim@163.com work-da intersting:oingoang'
print(re.split(r'[@\-:\s+]', s))
# ['sim', '163.com', 'work', 'da', 'intersting', 'oingoang']
网友评论