1. 程式人生 > >Python--每日正則(五)

Python--每日正則(五)

import re

# s="i love you not because of who you are, but because of who i am when i am with you"
#
# pattern = '(\w)\w*'
# res = re.match(pattern, s)
# print(res)
#
# danci_list = s.split(' ')
# print(danci_list)

# for word in danci_list:
#     print(word[0])


# 2、匹配一行文字中的  所有開頭的  所有數字內容
# s="i love you not because 12sd 34er 56df e4 54434"
# pattern = '\s[0-9]+'
# res_list = re.findall(pattern, s)
# print(res_list)


# line = 'aaa3j'
# patten = r"^[a2-9tjqk]{5}$"
# match_res = re.match(patten, line)
# if match_res:
#     print(match_res)
#     print('ojbk')
# else:
#     print('不歐克')


# patten = r'.*(.).*\1'
# line = '129456789a'
# pattern = r'\s(\w*?ly)'
# text = "He was carefully disguised but captured quickly by police."
# res_list = re.findall(pattern, text)
# print(type(res_list))
#
# print(res_list)
# match_res = re.search(pattern, text)
# if match_res:
#     print(match_res)
#     print(match_res.group(0)) #carefully
#     print(match_res.group(1)) #carefully
#     print('ojbk')
# else:
#     print('不歐克')


#  通訊錄

text = """
Ross McFluff: 834.345.1254 155 Elm Street
Ronald Heathmore: 892.345.3428 436 Finley Avenue
Frank Burger: 925.541.7625 662 South Dogwood Way
Heather Albrecht: 548.326.4584 919 Park Place
"""

{'Ross McFluff': 'Elm Street', 'Ronald Heathmore': 'Finley Avenue'}

tongxunlu_list = text.split('\n')
print(tongxunlu_list)
tongxunlu_dict = {}
for item in tongxunlu_list:
    if item != '':
        pattern = r'(.*):.*\d+\s(.*)'
        match_res = re.search(pattern, item)
        print('0',match_res.group(0))
        if match_res:
            tongxunlu_dict[match_res.group(1)] = match_res.group(2)
print(tongxunlu_dict)