-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_re.py
More file actions
55 lines (43 loc) · 1.75 KB
/
Copy path4_re.py
File metadata and controls
55 lines (43 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 주민등록번호
# 000000-0000000
# abcdef-1111111
# 이메일주소
# nudocoding@gmail.com
# nudocoding@gmail@gmail.com
# 차량번호
# 11가 1234
# 123가 1234
# IP주소
# 192.168.0.1
# 1000.2000.3000.4000
import re
# abcd, book, desk
# ca?e
# care, cafe, case, cave, cake
# caae, cabe, cace, cade, ....
p = re.compile("ca.e")
# . (ca.e): 하나의 문자를 의미함 > care, cafe, case (o) | caffe (x)
# ^ (^de): 문자열의 시작을 의미함 > desk, destination (o) | fade (x)
# $ (se$): 문자열의 끝을 의미함 > case, base (o) | face (x)
def print_match(m):
if m:
print("m.group : ", m.group()) #일치하는 문자열 반환
print("m.string : ", m.string #입력받은 문자열
print("m.start : ", m.start())#일치하는 문자열의 시작 index
print("m.end : ", m.end())# 일치하는 문자열의 끝 index
print("m.span : ", m.span())# 일치하는 문자열의 시작 / 끝 index
else:
print("매칭되지 않음")
m = p.match("case") # match : 주어진 문자열의 처음부터 일치하는지 확인
# m = p.match("careless")
print_match(m)
# print(m.group()) #매치되지 않으면 에러가 발생
p.search("good care") # search : 주어진 문자열의 처음부터 일치하는게 있는지 확인
print_match(m)
lst = p.findall("careless") # findall : 일치하는 모든 것을 리스트 형태로 반환
print(lst)
#1. p = re.compile("원하는 형태")
#2. m = p.match("비교할 문자열") : 주어진 문자열의 처음부터 일치하는 확인
#3. m = p.search("비교할 문자열") : 주어진 문자열 중에 일치하는게 있는지 확인
#4. lst = p.findall("비교할 문자열") : 일치하는 모든 것을 "리스트" 형태로 반환
#원하는 형태 : 정규식