Skip to content

Commit d06c97e

Browse files
author
ys88sem.kim
committed
add 2022-04-12 regular expression chapter7
1 parent 373b4aa commit d06c97e

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎practical_python/chapter7.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import re
2+
3+
phoneNumRegex = re.compile(r'(\d{3})-(\d{4})-(\d{4})')
4+
mobile = phoneNumRegex.search('My number is 010-0000-0000') # 없으면 None
5+
if mobile != None :
6+
print('phone number found: ' + mobile.group()) # 실제 패턴 문자열 리턴
7+
print('phone number found: ' + mobile.group(0))
8+
print('phone number found: ' + mobile.group(1))
9+
print('phone number found: ' + mobile.group(2))
10+
print('phone number found: ' + mobile.group(3))
11+
12+
heroRegex = re.compile(r'Batman|Tina Fey')
13+
mo1 = heroRegex.search('Batman and Tina')
14+
print(mo1.group())
15+
mo2 = heroRegex.search('Tina Fey and Batman')
16+
print(mo2.group()) #뒤에 맞은 문자는 무시 한다
17+
18+
# 선택적 ?
19+
heroRegex = re.compile(r'Bat(wo)?man') # wo 는 선택적 패턴
20+
mo1 = heroRegex.search('Batman')
21+
print(mo1.group())
22+
mo2 = heroRegex.search('Batwoman')
23+
print(mo2.group())
24+
25+
# * <-- 0개 또는 그 이상과 일치 시키기
26+
heroRegex = re.compile(r'Bat(wo)*man') # wo 는 선택적 패턴
27+
mo1 = heroRegex.search('Batwowowoman')
28+
print(mo1.group())
29+
mo2 = heroRegex.search('Batwoman')
30+
print(mo2.group())
31+
32+
# + <-- 하나 또는 그 이상과 일치 시키기
33+
heroRegex = re.compile(r'Bat(wo)+man') # wo 는 선택적 패턴
34+
mo1 = heroRegex.search('Batwowowoman')
35+
print(mo1.group())
36+
mo2 = heroRegex.search('Batman')
37+
print(mo2 == None)
38+
39+
# 대괄호 -> 사용자 정의 문자 클래스
40+
vowelRegex = re.compile(r'[aeiouAEIOU]')
41+
print (vowelRegex.findall('RoboCop eats baby food. BABY FOOD'))
42+
43+
vowelRegex = re.compile(r'[^aeiouAEIOU]')
44+
print (vowelRegex.findall('RoboCop eats baby food. BABY FOOD'))
45+
46+
# 시작 기호 ^ (캐럿) $ 끝남 기호 달러
47+
wholeStringIsNum = re.compile(r'^\d+$')
48+
print(wholeStringIsNum.search('1234567890'))
49+
print(wholeStringIsNum.search('12345xyz67890') == None)
50+
print(wholeStringIsNum.search('12 34567890') == None)
51+
52+
53+
# . <-- 와일드 카드 문자 (줄바꿈 제외한 모든문자)
54+
atRegex = re.compile(r'.at')
55+
print(atRegex.findall('The cat in the hat sat on the flat mat.'))
56+
57+
# 점 별표로 모든것 일치
58+
nameRegex = re.compile(r'First Name: (.*) Last Name: (.*)')
59+
mo = nameRegex.search('First Name: Al Last Name: Sweigart')
60+
print (mo.group(1))
61+
print (mo.group(2))
62+
63+
# sub 메소드로 문자열 대체하기
64+
namesRegex = re.compile(r'Agent \w+')
65+
print(namesRegex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.'))

‎practical_python/test/test.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import unittest
2+
3+
class TestRegularExpression (unittest.TestCase):
4+
def test_string_length(self):
5+
self.assertEqual('foo'.upper(),'Foo')
6+

0 commit comments

Comments
 (0)