python

[python] ๋ฌธ์ž์—ด ์ •๊ทœํ‘œํ˜„์‹ ์ •๋ฆฌ

Meng's Computer 2021. 1. 14. 17:00

๐Ÿ“ข ๊ฐœ์š”

์ •๊ทœํ‘œํ˜„์‹(Regular Expressions) : ๋ณต์žกํ•œ ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ

  • ํŠน์ • ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์— ์ข…์†๋œ ๋ฌธ๋ฒ•์„ ๊ฐ€์ง„ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ์ด๋ผ๋ฉด ํญ๋„“๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ ๋ชจ๋“ˆ ์ค‘ re ๋ชจ๋“ˆ์ด ์ •๊ทœํ‘œํ˜„์‹์„ ์ง€์›

๐Ÿ‘‡ ์™œ ๋ฐฐ์›Œ์•ผ ํ• ๊นŒ ?

๋”๋ณด๊ธฐ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.
[ ๋ณด์•ˆ์„ ์œ„ํ•ด ๊ณ ๊ฐ ์ •๋ณด ์ค‘ ์ „ํ™”๋ฒˆํ˜ธ ๊ฐ€์šด๋ฐ ์ž๋ฆฌ์˜ ์ˆซ์ž๋Š” * ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜์„ธ์š”. ]

๊ณ ๊ฐ ์ •๋ณด๋Š” ์ด๋ฆ„, ์ฃผ๋ฏผ๋ฒˆํ˜ธ, ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋กœ ์ฃผ์–ด์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

text = '''
Elice 123456-1234567 010-1234-5678
Cheshire 345678-678901 01098765432
'''

์ด ๋ฌธ์ œ๋ฅผ ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ’€๋ ค๋ฉด ๋งค์šฐ ๋ณต์žกํ•˜๊ฒŒ ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1. ์ „์ฒด ํ…์ŠคํŠธ๋ฅผ ๊ณต๋ฐฑ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค.
  2. ๋‚˜๋ˆ„์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹์ธ์ง€ ์ ๊ฒ€ํ•œ๋‹ค.
  3. ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋‹ค์‹œ ๋‚˜๋ˆ„์–ด ๊ฐ€์šด๋ฐ ์ž๋ฆฌ์˜ ์ˆซ์ž๋ฅผ *๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
  4. ๋‚˜๋ˆˆ ๋ฌธ์ž์—ด์„ ๋‹ค์‹œ ํ•ฉ์ณ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์™„์„ฑํ•œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋‚˜๋ˆ„์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹์ธ์ง€ ์–ด๋–ป๊ฒŒ ์ ๊ฒ€ํ• ๊นŒ์š”? ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์šด๋ฐ ์ž๋ฆฌ์˜ ์ˆซ์ž๋Š” ์–ด๋–ป๊ฒŒ ๋‚˜๋ˆ„์–ด *๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋‹ค์‹œ ํ•ฉ์น ๊นŒ์š”?

์„ค์ƒ๊ฐ€์ƒ์œผ๋กœ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ตฌ๋ถ„๋ฌธ์ž์ธ -๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ ํ•ด๊ฒฐ์€ ๋”์šฑ ์‰ฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์ •๊ทœํ‘œํ˜„์‹์„ ์ด์šฉํ•˜๋ฉด ๋งค์šฐ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•ž์„œ ์˜ˆ๋กœ ๋“  ์ „ํ™”๋ฒˆํ˜ธ๊ฐ™์ด, ์›ํ•˜๋Š” ํ˜•์‹์˜ ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•  ๋•Œ ๋ฉ”ํƒ€๋ฌธ์ž์™€ ์ˆ˜๋Ÿ‰์ž ๋“ฑ ๋‹ค์–‘ํ•œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งค์น˜ํ•˜๊ณ , ๊ทธ๋ฃนํ•‘์„ ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ๊ณจ๋ผ๋‚ด๊ณ  re๋ชจ๋“ˆ์˜ ๋ฉ”์„œ๋“œ๋กœ ๋ฌธ์ž์—ด์„ ์ˆ˜์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“ข re ๋ชจ๋“ˆ

import re	
 

re — Regular expression operations — Python 3.9.1 documentation

This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings (str) as well as 8-bit strings (bytes). However, Unicode strings and 8-bit strings cannot be mixed:

docs.python.org

 

๐Ÿ“ข ์ •๊ทœ์‹ ๊ฒ€์‚ฌ ํ•จ์ˆ˜

๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ์ •๊ทœ์‹์œผ๋กœ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ 4๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

ํ•จ์ˆ˜ ์ด๋ฆ„ ๊ธฐ๋Šฅ
re.match(pattern, string) string ์‹œ์ž‘ ๋ถ€๋ถ„๋ถ€ํ„ฐ ํŒจํ„ด์ด ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ MatchObject๋ฅผ ๋ฐ˜ํ™˜ํ•จ.
re.search(pattern, string) string ์ „์ฒด์—์„œ pattern์ด ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ MatchObject๋ฅผ ๋ฐ˜ํ™˜ํ•จ.
re.findall(pattern, string) string ์ „์ฒด์—์„œ ํŒจํ„ด๊ณผ ๋งค์น˜๋˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์ฐพ์•„ list๋กœ ๋ฐ˜ํ™˜ํ•จ.
re.finditer(pattern, string) string ์ „์ฒด์—์„œ ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ iterater ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ.

 

๐Ÿ“ข ๋ฌธ์ž์—ด ์ˆ˜์ • ํ•จ์ˆ˜

re ๋ชจ๋“ˆ์—๋Š” ํŒจํ„ด๊ณผ ๋งค์น˜๋œ ๋ฌด์ž์—ด์„ ์ฐพ์•„์ค„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋“ค๋„ ์กด์žฌํ•œ๋‹ค.

ํ•จ์ˆ˜ ์ด๋ฆ„ ๊ธฐ๋Šฅ
re.sub(pattern, repl, string) string ์—์„œ pattern๊ณผ ๋งค์นญ๋˜๋Š” ๋ถ€๋ถ„์„ repl๋กœ ์ˆ˜์ •ํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•จ.
re.subn(pattern, repl, string) re.sub()๊ณผ ๋™์ผํ•˜์ง€๋งŒ, ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ (๊ฒฐ๊ณผ ๋ฌธ์ž์—ด, ๊ต์ฒด ํšŸ์ˆ˜)๊ผด์˜ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜ํ•จ.

 

๐Ÿ“ข ๋ฉ”ํƒ€ ๋ฌธ์ž

๋ฉ”ํƒ€ ๋ฌธ์ž๋Š” ํŠน์ •ํ•œ ๋ฌธ์ž ํ˜น์€ ๊ณ„์—ด์„ ํ‘œํ˜„ํ•˜๋Š” ์•ฝ์†๋œ "๊ธฐํ˜ธ" ์ด๋‹ค.
๋ฉ”ํƒ€ ๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ํŠน์ •ํ•œ ๊ทœ์น™์„ ๊ฐ€์ง„ ์—ฌ๋Ÿฌ ๋‹จ์–ด๋ฅผ ์งง๊ฒŒ ์••์ถœํ•  ์ˆ˜ ์žˆ์–ด ํŽธ๋ฆฌํ•˜๋‹ค.

์ฃผ์š” ๋ฉ”ํƒ€๋ฌธ์ž

  • [^ ] ์™€ ๊ฐ™์ด ๋ฌธ์žํด๋ž˜์Šค [ ] ์•ˆ์— ^์ด ์“ฐ์ด๋ฉด, ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์ด ์•„๋‹Œ, not ์— ์˜๋ฏธ๋กœ ์‚ฌ์šฉ
  • [ - ]๋ฌธ์žํด๋ž˜์Šค [ ] ์•ˆ์— - ๋ฅผ ๊ธฐ์ค€์œผ๋กœ, ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ์— ๋ฒ”์œ„๋กœ ์“ธ ๋ฌธ์ž๋ฅผ ์“ฐ๋ฉด, ๊ทธ ์‚ฌ์ด์— ๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž ์ „๋ถ€๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋ฐ˜๋“œ์‹œ ์™ผ์ชฝ์˜ ์œ ๋‹ˆ์ฝ”๋“œ(๋˜๋Š” ์•„์Šคํ‚ค์ฝ”๋“œ)๊ฐ’์ด ์˜ค๋ฅธ์ชฝ๋ณด๋‹ค ์ž‘์•„์•ผํ•œ๋‹ค.
  • โ˜… "(?i)" = "(?i) ํ”Œ๋ž˜๊ทธ" ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ํŠน์ • ํŒจํ„ด ์œ„์น˜์— ์ƒ๊ด€์—†์ด, ์ด ํ”Œ๋ž˜๊ทธ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด, ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์˜๋ฏธ

 

๐Ÿ“ข ์ˆ˜๋Ÿ‰์ž(Quantifier)

๋™์ผํ•œ ๊ธ€์ž๋‚˜ ํŒจํ„ด์ด ๋ฐ˜๋ณต๋  ๋•Œ, ๊ทธ๋Œ€๋กœ ์ •๊ทœํ‘œํ˜„์‹์„ ๋งŒ๋“ค๊ณ ์ž ํ•˜๋ฉด ์ƒ๋‹นํžˆ ๋ถˆํŽธํ•˜๋‹ค.
\d ์™€ \w๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ๊ฐ ์ˆซ์ž์™€ ๋ฌธ์ž๋ฅผ "ํ•œ ๊ธ€์ž"์”ฉ ๋งค์นญํ•ด์ค€๋‹ค.
์ด๋Š”, ์ด์–ด์ง€๋Š” ๋ฌธ์ž๋ฅผ ํŒจํ„ด์œผ๋กœ ๋งŒ๋“ค์–ด, "๋‹จ์–ด ๋‹จ์œ„"๋กœ ๋งค์นญํ•ด์•ผ ํ•  ๋•Œ ๋งค์šฐ ๋ถˆํŽธํ•˜๋‹ค.

์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•œ๊ฒŒ ์ˆ˜๋Ÿ‰์ž(Quantifier) ์ด๋‹ค.

์ฃผ์š” ์ˆ˜๋Ÿ‰์ž(Quantifier)

  • โ˜… ? = "์ˆ˜๋Ÿ‰์ž ํƒ์š•์„ฑ ์–ต์ œ" ์—ฐ์‚ฐ์ž

    ํ‘œ์—, "0๊ฐœ ๋˜๋Š” 1๊ฐœ"๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ˆ˜๋Ÿ‰์—ฐ์‚ฐ์ž ? ์™€๋Š” ๋‹ค๋ฅธ ํ‘œํ˜„์ด๋‹ค !! (์ฃผ์˜)
  • ์ˆ˜๋Ÿ‰์ž์˜ ์›๋ž˜ ํŠน์„ฑ์€ "ํƒ์š•์ "์ด๋ผ์„œ, ๋งค์นญ๋˜๋Š” "์ตœ๋Œ€ํ•œ ๊ธด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜"ํ•˜๋Š”๋ฐ, ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ ๊ฐ€๋Šฅํ•œ "์ตœ์†Œํ•œ"์˜ ์ˆ˜๋Ÿ‰๊ณผ ๋งค์นญํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

    ex) text = "<html><head><title>์ œ๋ชฉ</head></html>" ์ผ ๋•Œ


    1. pattern = "<.*>" ์ผ ๋•Œ

    ๐Ÿ‘‰ ["<html><head><title>์ œ๋ชฉ</head></html>"]  ์ „์ฒด ํ•˜๋‚˜๊ฐ€ ๋ฐ˜ํ™˜ ๋ ๊ฒƒ์ด๋‹ค. <์˜ ์‹œ์ž‘๊ณผ >์˜ ๋งจ๋์„ ๊ธฐ์ค€

    2. pattern = <.*?>" ์ผ ๋•Œ ( ์ˆ˜๋Ÿ‰์ž ํƒ์š• ์–ต์ œ ์‚ฌ์šฉ )

    ๐Ÿ‘‰ [ "<html>" , "<head>" , "<title>" , "</head>", "</html>" ] ๋ฐ˜ํ™˜. < >์˜ ์ตœ์†Œํ•œ์˜ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆˆ๋‹ค.

 

๐Ÿ“ข ๊ทธ๋ฃน

( ) ๋Š” ๊ทธ๋ฃน์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋ฃน์€ ์ „์ฒด ํŒจํ„ด ๋‚ด์—์„œ "ํ•˜๋‚˜๋กœ ๋ฌถ์—ฌ์ง€๋Š” ํŒจํ„ด"์„ ๋งํ•œ๋‹ค.
๊ทธ๋ฃน๊ณผ | ๋ฅผ ๊ฒฐํ•ฉํ•œ ํ˜•ํƒœ, ๋˜๋Š” ๊ทธ๋ฃน ๋’ค์— ์ˆ˜๋Ÿ‰์ž๋ฅผ ๋ถ™์ด๋Š” ํŒจํ„ด์œผ๋กœ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

์˜ˆ์‹œ

  • (e|a)lice ๋Š” elice, alice์™€ ๋งค์นญ๋ฉ๋‹ˆ๋‹ค.
  • (tom|pot)ato๋Š” tomato, potato์™€ ๋งค์นญ๋ฉ๋‹ˆ๋‹ค.
  • (base|kick){2} ์€ basebase, basekick, kickkick, kickbase ์™€ ๋งค์นญ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฃน ์ฐธ์กฐ ์˜ˆ์‹œ

  • ํŠน์ •๋ฌธ์ž์—ด = "(๊ทธ๋ฃน1)์ผ๋ผ์ผ๋ผ~(๊ทธ๋ฃน2)"  << ์˜ˆ์‹œ๊ฐ€ ์žˆ๋‹ค์น˜์ž.
  • โ˜… ์ด ๊ทธ๋ฃน์„ "์ฐธ์กฐ"ํ•  ๋•Œ, "\g<๊ทธ๋ฃน๋ฒˆํ˜ธ>" ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ๋‚ด๊ฐ€ ์ •์˜ํ–ˆ๋˜, ์ •๊ทœ์‹์˜ ๊ทธ๋ฃน์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฃน์˜ ์žฌ์‚ฌ์šฉ

ํ•œ ๋ฒˆ ๋งŒ๋“  ๊ทธ๋ฃน์€ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ๋“ค์–ด์ง„ ์ˆœ์„œ๋ถ€ํ„ฐ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ทธ๋ฃน์œผ๋กœ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋งค์น˜ํ•œ ๊ทธ๋ฃน์„ ํŒจํ„ด ๋‚ด์—์„œ ์žฌ์‚ฌ์šฉํ•˜๋ ค๋ฉด \\1๊ณผ ๊ฐ™์ด ๊ทธ๋ฃน ๋ฒˆํ˜ธ๋ฅผ ์ด์Šค์ผ€์ดํ”„ํ•˜์—ฌ ๋‚˜ํƒ€๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ

  • (to)ma\\1์€ tomato์™€ ๋งค์นญ๋ฉ๋‹ˆ๋‹ค. ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ž์—์„œ ๋งŒ๋“  ๊ทธ๋ฃน (to)๋ฅผ ๋’ค์—์„œ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

๊ทธ ์™ธ ( group( ) ๋ฉ”์†Œ๋“œ )

์ด์™ธ์—๋„ ๊ทธ๋ฃน์—๋Š” re ๋ชจ๋“ˆ์˜ match ๊ฐ์ฒด์— ์†ํ•ด์žˆ๋Š” group ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งค์นญ๋œ ๊ฒฐ๊ณผ ์ค‘ ์ผ๋ถ€ ๋‚ด์šฉ๋งŒ์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ์‚ฌ์šฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

text = "๋ชฉํ‘œํ…์ŠคํŠธ"
pattern = "์ •๊ทœํ‘œํ˜„์‹"
match_object = re.search(pattern, text)		# ๋งค์นญ๋œ "๊ฐ์ฒด"๋ฅผ ๋ฐ˜ํ™˜
match_object.group()	# ๋งค์นญ๋œ ๊ฐ์ฒด ๊ธฐ๋ฐ˜์œผ๋กœ, ๊ทธ๋ฃจํ•‘

 

๐Ÿ“ข ๋น„์บก์ณ๋ง๊ทธ๋ฃน

์˜ˆ๋ฅผ๋“ค์–ด, "tomato potato" ๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์žˆ๋‹ค.  ์—ฌ๊ธฐ์„œ, tomato ์™€ potato๋ฅผ ๋ฝ‘์•„๋‚ด๊ธฐ์œ„ํ•ด ์•ž์„ , "๊ทธ๋ฃน"์œผ๋กœ๋Š”"(tom|pot)ato"๋ฅผ ํŒจํ„ด์œผ๋กœ ๋„ฃ์„ ๊ฒƒ์ด๋‹ค. 

๐Ÿ‘‰ ๊ทธ๋Ÿฌ๋‚˜, ๊ฒฐ๊ณผ๋Š” ["tom", "pot"] ์œผ๋กœ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค.

โ˜… ์ด์œ ๋Š”, ํŒŒ์ด์ฌ ์ •๊ทœํ‘œํ˜„์‹์—์„œ ๊ทธ๋ฃน์œผ๋กœ "์บก์ณ" ํ•œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด, "์ด์™ธ์— ๋ถ€๋ถ„๋“ค์€ ์ถœ๋ ฅํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ"์ด๋‹ค.
์œ„ ํŒจํ„ด์—์„œ๋Š”, "ato"๋Š” ๊ทธ๋ฃน์— ์†ํ•ด์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, findall ํ•จ์ˆ˜์—์„œ ๋ˆ„๋ฝ(ํ•ด๋‹น๋˜์ง€ X)๋œ ๊ฒƒ์ด๋‹ค.

ํ•ด๊ฒฐ๋ฒ•

๐Ÿ‘‰ ๊ทธ๋ฃน ( ) ์•ž์— ?: ์„ ๋ถ™์ธ๋‹ค.  ( ex. "(?:tom|pot)ato)" )

๋‹จ, ๋น„์บก์ณ๋ง ๊ทธ๋ฃน์€ ์บก์ณ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ทธ๋ฃน์ด๊ธฐ ๋•Œ๋ฌธ์—, "์ฐธ์กฐํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค."

๋น„์บก์ณ๋ง๊ทธ๋ฃน์€ ํŒจํ„ด ๋ฌธ์ž๋“ค์„ ๋ฌถ๋˜, ๊ทธ๋ฃน ๋‹จ์œ„๋กœ ๋งค์นญ๋˜์ง€๋Š” ์•Š๊ฒŒ๋” ํ•œ๋‹ค.
๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์€ ๊ฒƒ๋“ค์„ "์ตœ์ข… ๊ฒฐ๊ณผ"์—์„œ ๋”ฐ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์ ์šฉํ•œ๋‹ค.
 

 

๐Ÿ“ Reference : Elice Academy

728x90
๋ฐ˜์‘ํ˜•