import re
# 匹配一行文字中所有开头的字母
s = 'i love you but you don\'t love me'
# \b\w findall
content = re.findall(r'\b\w', s)
print(content)
# 匹配一行文字中所有数字开头的内容
s1 = 'i 12love 2you 78but 980you 43don\'66t 89 love 43me'
content1 = re.findall(r'\b\d', s1)
print(content1)
# 匹配 只含数字和字母的行
s2 = 'i love you \n2222kkkk but \nfefe23 you dont love \n234324dd'
content2 = re.findall(r'\w+', s2, re.M)
print(content2)
# 写一个正则表达式,使其能匹配一下字符 'bit','bat','but','hat','hit','hut'
s3 = "'bit','bat','but','hat','hit','hut'"
content3 = re.findall(r'..t', s3)
print(content3)
# 提取每行中完整的年月日和时间段
s3 = 'se2323 1987-10-10 22:44:55 e4343 2018-10-20 09:47:54'
content4 = re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', s3)
print(content4)
# 提取电子邮件格式
s4 = """[email protected] [email protected] baidu.com 999.com [email protected]"""
content5 = re.findall(r'\w+@\w+.com', s4)
print(content5)
# 把以上合法的电子邮件地址替换成自己的电子邮件地址
content6 = re.sub(r'\w+@\w+.com', '[email protected]', s4)
print(content6)
# 使用正则提取字符串中的单词
s5 = 'i love you not because who 233 of 890sdx not'
content7 = re.match(r'\b[a-zA-Z]+\b', s5)
# 此代码与以上代码相等
# content7 = re.search(r'\b^[a-zA-Z]+\b', s5)
print(content7.group())
['i', 'l', 'y', 'b', 'y', 'd', 't', 'l', 'm']
['1', '2', '7', '9', '4', '6', '8', '4']
['i', 'love', 'you', '2222kkkk', 'but', 'fefe23', 'you', 'dont', 'love', '234324dd']
['bit', 'bat', 'but', 'hat', 'hit', 'hut']
['1987-10-10 22:44:55', '2018-10-20 09:47:54']
ruochen@163.com ruochen@163.com baidu.com 999.com ruochen@163.com
i
正文完