一,
- # 匹配一行文字中所有开头的字母
- import re
- s = 'i love you but you don\'t love me'
- # \b\m findall
- content = re.findall(r'\w\b', s)
- print(content)
- # 匹配一行文字中所有数字开头的内容
- s1 = 'i 22love 33you 44but 5you don\'t66 7love 99me'
- # \d
- content = re.findall(r'\b\d', s1)
- print(content)
- # 匹配 只含数字和字母的行
- s2 = 'i love you \n2222kkkk but \ndfefe23 you dont love \n23243dd'
- content = re.findall(r'\w+', s2, re.M)
- print(content)
二, 写一个正则表达式, 使其能匹配以下字符'bit','but','hat','hit','hut'
- # 写一个正则表达式, 使其能匹配以下字符'bit','but','hat','hit','hut'
- s3 = "'bit', 'bat', 'but', 'hat', 'hit', 'hut'"
- content = re.findall(r'...t', s3)
- print(content)
三,
- # 提取电子邮件格式
- s4 = """[email protected] [email protected] baidu.com 999.com [email protected]"""
- content = re.findall(r'\[email protected]\w+.com', s4)
- print(content)
四,
- # 使用正则提取字符串中的单词
- s5 = "i love you not because who 233 of 890sdx not"
- content = re.findall(r'\b[a-zA-Z]+\b', s5)
- print(content)
五,
- # 使用正则提取字符串中的单词
- s5 = "i love you not because who 233 of 890sdx not"
- #content = re.findall(r'\b[a-zA-Z]+\b', s5)
- #print(content)
- content = re.match(r'[a-zA-Z]+\b', s5)
- content = re.search(r'^[a-zA-Z+\b]', s5)
- print(content.group())
六,
来源: http://www.bubuko.com/infodetail-3102220.html