Skip to content

Instantly share code, notes, and snippets.

@awesometype
Created July 10, 2017 08:19
# line = 'asdd fr frfs; fr; frs frss '
# import re
# res = re.split(r'[;,\s]\s*',line)
# print(res)
# filename = 'spam.txt'
# print(filename.endswith('.txt'))
# print(filename.startswith('file:'))
# url = 'http://www.python.org'
# print(url.startswith('http:'))
# import os
# filenames = os.listdir('.')
# print(filenames)
#
# [print(name) for name in filenames if name.startswith(('day01','tk1'))]
# import requests
#
# def read_data(name):
# if name.startswith(('http:','https','ftp')):
# return requests.get(name).text
# else:
# with open(name) as f:
# return f.read()
#
# print(read_data('day01.py'))
# print(read_data('http://www.baidu.com'))
# from fnmatch import fnmatch,fnmatchcase
# import os
#
# print(fnmatch('foo.txt','*.txt'))
# print(fnmatch('foo.txt','?oo.txt'))
# filenames = os.listdir()
# print(filenames)
# [print(name) for name in filenames if fnmatch(name,'day*.py')]
# addresses = [
# '安徽南京',
# '安徽北京',
# '54安徽天津',
# '554安徽深圳',
# '安徽广州',
# ]
#
# from fnmatch import fnmatch,fnmatchcase
#
# [print(name) for name in addresses if fnmatch(name,'安徽*')]
# print()
# [print(name) for name in addresses if fnmatch(name,'*京')]
# print()
# [print(name) for name in addresses if fnmatch(name,'5[0-9]安*')]
# text = 'yeah ,no,ha,no'
# print(text.find('no'))
# text = '你好 安徽'
# text2 = text.replace('安徽','世界')
# print(text2)
# text = '今天是7/6/2017,下周一是7/10/2017'
# import re
# res = re.sub(r'(\d+)/(\d+)/(\d+)',r'\3-\1-\2',text)
# print(res)
#如果你打算用相同的模式做多次替换,考虑先编译它来提升性能。
import re
# datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
# res = datepat.sub(r'\3-\1-\2',text)
# print(res)
#
# from calendar import month_abbr
# def change_date(m):
# mon_name = month_abbr[int(m.group(1))]
# return '{}{}{}'.format(m.group(2),mon_name,m.group(3))
#
# res = datepat.sub(change_date,text)
# print(res)
# text = 'UPPER PYTHON,lower python,Mixed Python'
# res = re.findall('python',text,flags=re.IGNORECASE)
# print(res)
# res2 = re.sub('python','snake',text,flags=re.IGNORECASE)
# print(res2)
# text = 'UPPER PYTHON,lower python,Mixed Python'
# def matchcase(word):
# def replace(m):
# text = m.group()
# print(type(text))
# if text.isupper():
# return word.upper()
# elif text.islower():
# return word.lower()
# elif text[0].isupper():
# return word.capitalize()
# else:
# return word
# return replace#将这个函数返回
#
#
# res3 = re.sub('python',matchcase('snake'),text,flags=re.IGNORECASE)
# print(res3)
#2.7最短匹配模式
str_pat = re.compile(r'\"(.*)"')
text1 = 'Computer says "no."'
print(str_pat.findall(text1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment