์ •๊ทœ ํ‘œํ˜„์‹์€ ๋‹ค์–‘ํ•œ ์–ธ์–ด์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด ๊ธ€์—์„œ๋Š” ํŒŒ์ด์ฌ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ™•์ธํ•ด ๋ณด๋„๋ก ํ•˜์ž.

import re
p = re.compile('ab*')

re.compile์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •๊ทœ ํ‘œํ˜„์‹(์œ„ ์˜ˆ์—์„œ๋Š” ab*)์„ ์ปดํŒŒ์ผํ•œ๋‹ค. re.compile์˜ ๊ฒฐ๊ณผ๋กœ ๋Œ๋ ค์ฃผ๋Š” ๊ฐ์ฒด p(์ปดํŒŒ์ผ๋œ ํŒจํ„ด ๊ฐ์ฒด)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ ์ดํ›„์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

  • ์ •๊ทœ์‹์„ ์ปดํŒŒ์ผํ•  ๋•Œ ํŠน์ • ์˜ต์…˜์„ ์ฃผ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•œ๋ฐ, ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋’ค์—์„œ ์ž์„ธํžˆ ์‚ดํŽด๋ณธ๋‹ค.
  • ํŒจํ„ด์ด๋ž€ ์ •๊ทœ์‹์„ ์ปดํŒŒ์ผํ•œ ๊ฒฐ๊ณผ์ด๋‹ค.

Method

|Method|๋ชฉ์ | |:----:|::----| |match()|๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€ ์กฐ์‚ฌํ•œ๋‹ค.| |search()|๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€ ์กฐ์‚ฌํ•œ๋‹ค.| |findall()|์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด(substring)์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋Œ๋ ค์ค€๋‹ค.| |finditer()|์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด(substring)์„ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋กœ ๋Œ๋ ค์ค€๋‹ค.|

match, search๋Š” ์ •๊ทœ์‹๊ณผ ๋งค์น˜๋  ๋•Œ๋Š” match ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ฃผ๊ณ , ๋งค์น˜๋˜์ง€ ์•Š์„ ๋•Œ๋Š” None์„ ๋Œ๋ ค์ค€๋‹ค. ๊ฐ๊ฐ์˜ ์˜ˆ๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์˜ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋จผ์ € ์ˆ˜ํ–‰ํ•˜์ž.

import re
p = re.compile('[a-z]+')

Match

๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€ ์กฐ์‚ฌํ•œ๋‹ค.

>>> m = p.match("python")
>>> print(m)
<_sre.SRE_Match object at 0x01F3F9F8>Z

โ€œpythonโ€ ๋ฌธ์ž์—ด์€ [a-z]+ ์ •๊ทœ์‹์— ๋ถ€ํ•ฉ๋˜๋ฏ€๋กœ match ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ค€๋‹ค.

>>> m = p.match("3 python")
>>> print(m)
None

โ€œ3 pythonโ€ ๋ฌธ์ž์—ด์€ ์ฒ˜์Œ์— ๋‚˜์˜ค๋Š” ๋ฌธ์ž 3์ด ์ •๊ทœ์‹ [a-z]+์— ๋ถ€ํ•ฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ None์„ ๋Œ๋ ค์ค€๋‹ค. match์˜ ๊ฒฐ๊ณผ๋กœ match ๊ฐ์ฒด ๋˜๋Š” None์„ ๋Œ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ด์ฌ ์ •๊ทœ์‹ ํ”„๋กœ๊ทธ๋žจ์€ ๋ณดํ†ต ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ๋ฆ„์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

p = re.compile(์ •๊ทœํ‘œํ˜„์‹)
m = p.match( 'string goes here' )
if m:
    print('Match found: ', m.group())
else:
    print('No match')

๊ฒ€์ƒ‰๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š”์ง€ ์กฐ์‚ฌํ•œ๋‹ค.

>>> m = p.search("python")
>>> print(m)
<_sre.SRE_Match object at 0x01F3FA68>

match์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ์ด๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์˜ ์˜ˆ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๋‹ค.

>>> m = p.search("3 python")
>>> print(m)
<_sre.SRE_Match object at 0x01F3FA30>

โ€œ3 pythonโ€ ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” โ€œ3โ€์ด์ง€๋งŒ search๋Š” ๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— โ€œ3 โ€ ์ดํ›„์˜ โ€œpythonโ€ ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜๋œ๋‹ค. ์ด๋ ‡๋“ฏ match ๋ฉ”์„œ๋“œ์™€ search ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ• ์ง€์˜ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

findall

์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด(substring)์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋Œ๋ ค์ค€๋‹ค.

>>> result = p.findall("life is too short")
>>> print(result)
['life', 'is', 'too', 'short']

โ€œlife is too shortโ€ ๋ฌธ์ž์—ด์˜ โ€˜lifeโ€™, โ€˜isโ€™, โ€˜tooโ€™, โ€˜shortโ€™ ๋‹จ์–ด๋ฅผ ๊ฐ๊ฐ [a-z]+ ์ •๊ทœ์‹๊ณผ ๋งค์น˜ํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋Œ๋ ค์ค€๋‹ค.

finditer

์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด(substring)์„ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋กœ ๋Œ๋ ค์ค€๋‹ค.

findall๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฌธ์ž์—ด์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์ด ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ค‘์—์„œ, ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋‹จ์–ด๋“ค์— ๋Œ€ํ•ด ๋‹ค์‹œํ•œ๋ฒˆ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰ํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ findall์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋ฑ‰๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ์ด๋Ÿฌํ•œ ํ•„์š”์„ฑ์—์„œ finditer method๋Š” ๊ต‰์žฅํžˆ ์œ ์šฉํ•˜๋‹ค.

>>> result = p.finditer("life is too short")
>>> print(result)
<callable_iterator object at 0x01F5E390>
>>> for r in result: print(r)
...
<_sre.SRE_Match object at 0x01F3F9F8>
<_sre.SRE_Match object at 0x01F3FAD8>
<_sre.SRE_Match object at 0x01F3FAA0>
<_sre.SRE_Match object at 0x01F3F9F8>

match ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค๋ฅธ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

Reference