import re
pattern = r'\d\d\d-\d\d\d-\d\d\d\d'
s = input('Enter tel. number: ')
if re.match(pattern, s):
print('Number accepted.')
else:
print('Incorrect format.')
Enter tel. number: 222 222 2222 Incorrect format.
import re
pattern = r'\d\d\d[ -]\d\d\d[ -]\d\d\d\d$'
s = input('Enter tel. number: ')
if re.match(pattern, s):
print('Number accepted.')
else:
print('Incorrect format.')
Enter tel. number: 333 333 3333 Number accepted.
import re
pattern = r'\d\d\d[ -]\d\d\d[ -]\d\d\d\d'
s = input('Enter tel. number: ')
if re.fullmatch(pattern, s):
print('Number accepted.')
else:
print('Incorrect format.')
Enter tel. number: 111 111 1111 Number accepted.
import re
pattern = r'\d\d\d-\d\d-\d\d\d\d$'
s = input('Enter SSN: ')
if re.match(pattern, s):
print('Number accepted.')
else:
print('Incorrect format.')
Enter SSN: 111-11-1111 Number accepted.
import re
reg1 = re.compile(r'ca*b$') # 패턴을 컴파일한다!
def test_item(s):
if re.match(reg1, s):
print(s, 'is a match.')
else:
print(s, 'is not a match!')
test_item('caab')
test_item('caaxxb')
caab is a match. caaxxb is not a match!
if re.match('m*ack', 'Mack the Knife', re.IGNORECASE):
print ('Success.')
Success.
if re.match('m*ack', 'Mack the Knife', re.I):
print ('Success.')
Success.
if re.match('m*ack', 'Mack the Knife', re.I | re.DEBUG):
print ('Success.')
MAX_REPEAT 0 MAXREPEAT LITERAL 109 LITERAL 97 LITERAL 99 LITERAL 107 0. INFO 4 0b0 3 MAXREPEAT (to 5) 5: REPEAT_ONE 6 0 MAXREPEAT (to 12) 9. LITERAL_UNI_IGNORE 0x6d ('m') 11. SUCCESS 12: LITERAL_UNI_IGNORE 0x61 ('a') 14. LITERAL_UNI_IGNORE 0x63 ('c') 16. LITERAL_UNI_IGNORE 0x6b ('k') 18. SUCCESS Success.
import re
if re.match(r'[+*^/-]', '^'):
print('Success!')
Success!
import re
if re.match(r'[^+*^/-]', '^'):
print('Success!')
import re
pat = r'c.*t'
if re.match(pat, 'cat'):
print('Success!')
Success!
import re
pat1 = r'(\w|[@#$%^&*!]){8,}$'
pat2 = r'.*\d'
pat3 = r'.*[a-zA-Z]'
pat4 = r'.*[@#$%^$*]'
def verify_passwd(s):
b = (re.match(pat1, s) and re.match(pat2, s) and
re.match(pat3, s) and re.match(pat4, s))
return bool(b)
import re
pat = r'(a+)(b+)(c+)'
m = re.match(pat, 'abbcccee')
print(m.group(0))
print(m.group(1))
print(m.group(2))
print(m.group(3))
abbccc a bb ccc
import re
pat = r'(a+)(b+)(c+)'
m = re.match(pat, 'abbcccee')
for i in range(m.lastindex + 1):
print(i, '. ', m.group(i), sep='')
0. abbccc 1. a 2. bb 3. ccc
import re
m = re.search(r'\d{2,}', '1 set of 23 owls, 999 doves.')
print('"', m.group(), '" found at ', m.span(), sep='')
"23" found at (9, 11)
import re
s = '1 set of 23 owls, 999 doves.'
print(re.findall(r'\d+', s))
['1', '23', '999']
import re
s = 'What is 1,000.5 times 3 times 2,000?'
print(re.findall(r'\d[0-9,.]*', s))
['1,000.5', '3', '2,000']
s = 'I do not use sophisticated, multisyllabic words!'
print(re.findall(r'\w{6,}', s))
['sophisticated', 'multisyllabic']
import re
s = '12 15+3 100-*'
print(re.findall(r'[+*/-]|\w+', s))
['12', '15', '+', '3', '100', '-', '*']
pat = r'\d{1,3}(,\d{3})*(\.\d*)?'
print(re.findall(pat, '12,000 monkeys and 55.5 cats.'))
[(',000', ''), ('', '.5')]
pat = r'(\d{1,3}(,\d{3})*(\.\d*)?)'
lst = re.findall(pat, '12,000 monkeys on 55.5 cats.')
for item in lst:
print(item[0])
12,000 55.5
import re
s = 'The cow jumped over the the moon.'
m = re.search(r'(\w+) \1', s)
print(m.group(), '...found at', m.span())
the the ...found at (20, 27)
s = 'The United States of of America.'
m = re.search(r'(\w+) \1', s)
print(m.group(), '...found at', m.span())
of of ...found at (18, 23)
s = 'The the cow jumped over the the moon.'
m = re.search(r'(\w+) \1', s, flags=re.I)
print(m.group(), '...found at', m.span())
The the ...found at (0, 7)
import re
s = 'Get me a new dog to befriend my dog.'
s2 = re.sub('dog', 'cat', s)
print(s2)
Get me a new cat to befriend my cat.
s = 'The the cow jumped over over the moon.'
s2 = re.sub(r'(\w+) \1', r'\1', s, flags=re.I)
print(s2)
The cow jumped over the moon.