In [5]:
line = 'asdf fjdk; afed, fjedk, asdef , foo'
import re
line_new = re.split(r'[;,\s]\s*',line)
In [4]:
line_new
Out[4]:
In [7]:
fields = re.split(r'(;|,|\s)\s*',line)
fields
Out[7]:
In [9]:
values = fields[::2]
delimiters = fields[1::2] + ['']
values
Out[9]:
In [10]:
delimiters
Out[10]:
In [11]:
# Reform the line using the same delimiters
''.join(v + d for v, d in zip(values, delimiters))
Out[11]:
In [12]:
re.split('(?:,|;|\s)\s*',line)
Out[12]:
In [3]:
filename = 'spm.txt'
filename.endswith('.txt')
Out[3]:
In [4]:
filename.startswith('spa')
Out[4]:
In [5]:
url = 'http://www.python.prg'
url.startswith('http:')
Out[5]:
In [6]:
import os
filenames = os.listdir('.')
filenames
Out[6]:
In [7]:
[name for name in filenames if name.endswith(('.ipynb','.py'))]
Out[7]:
In [8]:
any(name.endswith('.py') for name in filenames)
Out[8]:
In [11]:
choices = ['htttp:','ftp:']
url.startswith(choices)
In [12]:
url.startswith(tuple(choices))
Out[12]:
In [13]:
filename[-4:] == '.txt'
Out[13]:
In [14]:
url[:5] == 'http:' or url[:6] == 'https:' or url[:4] == 'ftp:'
Out[14]:
In [20]:
import re
re.match('http:|https:|ftp:',url)
Out[20]:
In [1]:
if any(name.endsw)
In [2]:
from fnmatch import fnmatch,fnmatchcase
fnmatch('foo.txt','*.txt')
Out[2]:
In [3]:
fnmatch('foo.txt','?oo.txt')
Out[3]:
In [4]:
fnmatch('Data45.csv','Dat[0-9]*')
Out[4]:
In [5]:
fnmatch('Dat45.csv','Dat[0-9]*')
Out[5]:
In [6]:
names = ['Dat1.csv','Dat2.csv','Dat3.csv','config.ini','foo.py']
[name for name in names if fnmatch(name,'Dat*.csv')]
Out[6]:
In [8]:
fnmatchcase('foo.txt','*.TXT')
Out[8]:
In [9]:
address = [
'5412 N CLARK ST',
'1060 W ADDISON ST',
'1039 W GRANVILLE AVE',
'2122 N CLARK ST',
'4802 N BROADWAY',
]
[addr for addr in address if fnmatchcase(addr,'* ST')]
Out[9]:
In [11]:
[addr for addr in address if fnmatchcase(addr,'54[0-9][0-9] *CLARK*')]
Out[11]:
In [1]:
text = 'yeah, but no,but yeah,but no,but yeah'
# Exact match
b0 = text == 'yeah'
# Match at start or end
b1 = text.startswith('yeah')
b2 = text.endswith('yeah')
# Search for the location of the first occurrence
b3 = text.find('no')
boolean = [b0,b1,b2,b3]
print(boolean)
In [2]:
text1 = '11/27/2013'
text2 = 'Nov 27,2012'
import re
# Simple matching: \d+ means match one or more digits
for text in (text1,text2):
if re.match(r'\d+/\d+/\d+',text):
print('Match yes!')
else:
print('Match no!')
In [2]:
import re
text1 = '11/27/2013'
text2 = 'Nov 27,2012'
datepat = re.compile(r'\d+/\d+/\d+')
for text in [text1,text2]:
if datepat.match(text):
print('yes')
else:
print('no')
In [3]:
text = 'Today is 11/27/2012,pycon starts 3/12/2013'
datepat.findall(text)
Out[3]:
In [5]:
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
m = datepat.match('11/27/1009')
m
Out[5]:
In [6]:
m.group(0)
Out[6]:
In [7]:
m.group(1) + m.group(2) + m.group(3)
Out[7]:
In [11]:
from functools import reduce
def addd(x,y):
return x + y
reduce(addd,m.groups())
Out[11]:
In [12]:
month,day,year = m.groups()
In [13]:
month + day + year
Out[13]:
In [14]:
datepat.findall(text)
Out[14]:
In [15]:
for month,day,year in datepat.findall(text):
print('{}-{}-{}'.format(year,month,year))
In [16]:
for m in datepat.finditer(text):
print(m.groups())
In [17]:
m = datepat.match('11/27/2013abcd')
m.groups()
Out[17]:
In [18]:
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
datepat.match('11/27/2013abc') == datepat.match('11/27/2013')
Out[18]:
In [19]:
re.findall(r'(\d+)/(\d+)/(\d+)',text)
Out[19]:
In [2]:
text = 'yeah,but nol,but yeah,byt no,but yeah'
text.replace('yeah','yep')
Out[2]:
In [3]:
text = 'Today is 11/27/2012,pycon starts 4/12/2021'
import re
re.sub(r'(\d+)/(\d+)/(\d+)',r'\3-\1-\2',text)
Out[3]:
In [ ]:
datept = re.compile(r'(\d+)/(\d+)/(\d+)')
datept