ππ» νμ΄μ¬μμ μ κ·ννμμ νμ©νλ €λ©΄?
import re
p = re.compile('ab*')
matchμ searchλ μ κ·μκ³Ό 맀μΉλ λλ κ°μ²΄λ₯Ό λλ €μ£Όκ³ , 맀μΉλμ§ μμ λλ Noneμ λλ €μ€λ€!
p = re.compile(μ κ·ννμ)
m = p.match( 'string goes here' )
if m:
print('Match found: ', m.group())
else:
print('No match')
---------------------------
match μμ.
import re
p = re.compile('[a-z]')
m = p.match('alpha123')
if m:
print('Match found: ', m.group())
else:
print('No match') #result : a
import re
p = re.compile('alp')
m = p.match('p alp')
if m:
print('Match found: ', m.group())
else:
print('No match') #result: no match
-----------------------------
search μμ
import re
p = re.compile('alp')
m = p.search('p alp')
if m:
print('Match found: ', m.group())
else:
print('No match') #result : alp
-----------------------------
findall μμ
import re
p = re.compile('[a-i]')
result = p.findall("life is too short")
print(result) #result : ['i', 'f', 'e', 'i', 'h'] 리μ€νΈλ‘ λλ €μ€
------------------------------
finditer μμ
import re
p = re.compile('[a-i]')
result = p.finditer("life is too short")
print(result)
for r in result : print(r)
#resultππ» : λ°λ³΅ κ°λ₯ν κ°μ²΄λ‘ λλ €μ€
<callable_iterator object at 0x000001BC87165F48>
<re.Match object; span=(1, 2), match='i'>
<re.Match object; span=(2, 3), match='f'>
<re.Match object; span=(3, 4), match='e'>
<re.Match object; span=(5, 6), match='i'>
<re.Match object; span=(13, 14), match='h'>
method | λͺ©μ |
---|---|
group() | 맀μΉλ λ¬Έμμ΄μ λλ €μ€λ€ |
start() | 맀μΉλ λ¬Έμμ΄μ μμμμΉλ₯Ό λλ €μ€λ€ |
end() | 맀μΉλ λ¬Έμμ΄μ λμμΉλ₯Ό λλ €μ€λ€ |
span() | 맀μΉλ λ¬Έμμ΄μ (μμ,λ)μ ν΄λΉνλ ννμ λλ €μ€λ€ |
μμ
>>> m = p.match("python")
>>> m.group()
'python'
>>> m.start()
0
>>> m.end()
6
>>> m.span()
(0, 6)
* stratμ κ°μ νμ 0μΌκ²! μ?! matchλ©μλλ νμ λ¬Έμμ΄μ μμλΆν° μ‘°μ¬νκΈ° λλ¬Έ!
μΆμ½νν
p = re.compile('[a-z]+')
m = p.match("python")
λ₯Ό μΆμ½νλ©΄
m = re.match('[a-z]+', "python")
- νλ² λ§λ ν¨ν΄ κ°μ²΄λ₯Ό μ¬λ¬λ² μ¬μ©ν΄μΌν λλ
re.compile
λ°©λ²μ μ°μ!