-
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
/
4_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("๋น๊ตํ ๋ฌธ์์ด") : ์ผ์นํ๋ ๋ชจ๋ ๊ฒ์ "๋ฆฌ์คํธ" ํํ๋ก ๋ฐํ
#์ํ๋ ํํ : ์ ๊ท์