import re
p = re.compile(r"정규표현식")
p = re.compile('python')
m = p.match("python")
print(m)
# <re.Match object; span=(0, 6), match='python'>
m = re.match(p, "python")
print(m)
# <re.Match object; span=(0, 6), match='python'>
m = p.match("python3.0ver")
print(m)
# <re.Match object; span=(0, 6), match='python'>
m = p.match("version--python3.0")
print(m)
# None
p = re.compile('py')
m = p.findall("pypy, python3.0, python3.8")
print(m)
print(type(m))
# ['py', 'py', 'py', 'py']
# <class 'list'>
p = re.compile('python')
m = p.finditer("pypy, python3.0, python3.8")
print([s for s in m])
print(type(m))
# [<re.Match object; span=(6, 12), match='python'>, <re.Match object; span=(17, 23), match='python'>]
# <class 'callable_iterator'>
print(re.split('(,)', "1,2,3,4,5"))
print(re.split(',', "1,2,3,4,5"))
# ['1', ',', '2', ',', '3', ',', '4', ',', '5']
# ['1', '2', '3', '4', '5']
print(re.split('(&)|(and)', "1&2and3and4&5"))
print(re.split('&|and', "1&2and3and4&5"))
# ['1', '&', None, '2', None, 'and', '3', None, 'and', '4', '&', None, '5']
# ['1', '2', '3', '4', '5']
p = re.compile('(blue|white|red)')
p.sub('colour', 'blue socks and red shoes')
# 'colour socks and colour shoes'
def hexrepl(match):
value = int(match.group())
return hex(value)
p = re.compile(r'\d+')
p.sub(hexrepl, 'Call 65490 for printing, 49152 for user code.')
# 'Call 0xffd2 for printing, 0xc000 for user code.'
p = re.compile('(blue|white|red)')
p.subn( 'colour', 'blue socks and red shoes')
# ('colour socks and colour shoes', 2)
리턴
m = p.search("3 python")
m.group()
# 'python'
m.start()
# 2
m.end()
# 8
m.span()
# (2, 8)
m = re.match('(\d{2})-(\d{3,4})-(\d{4})', '02-123-1234')
print(m.groups())
print(m.group())
print(m.group(0))
print(m.group(1))
print(m.group(2))
# ('02', '123', '1234')
# 02-123-1234
# 02-123-1234
# 02
# 123
result2 = re.match('\d{2}-\d{3,4}-\d{4}', '02-123-1234')
print(result2.groups())
print(result2.group())
# ()
# 02-123-1234
s = "abc123abc45"
p = re.compile('bc')
result = p.findall(s)
print(result)
# ['bc', 'bc']
print("개행 \n 됐다")
print("\\")
print("따옴표 \' 또는 \" 를 사용하는예")
# 개행
# 됐다
# \
# 따옴표 ' 또는 " 를 사용하는예
print("\\\\"== r"\\")
# True
print('"', "'")
print(''' 따옴표 '와 쌍따옴표" ''')
# " '
# 따옴표 '와 쌍따옴표"
print("\"")
print(r"\"")
# "
# \"
print(len("\""))
print(len(r"\""))
# 1
# 2
print(r"abc\")
# Error
print(r"abc\\")
# abc\\
import re
s = "\\good\\"
# \good\
p = re.compile('\\\\')
result = p.findall(s)
print(result)
# ['\\', '\\']
s = "abc123de45"
p = re.compile('\\d+')
p = re.compile(r'\d+') # 같은 표현
result = p.findall(s)
print(result)
# ['123', '45']
import re
p = re.compile(r"(\w+)\s+((?P<dialing>\d+)[-]\d+[-]\d+)")
m = p.search("park 010-1234-1234")
print(m.group(1))
print(m.group(2))
print(m.group("dialing"))
# park
# 010-1234-1234
# 010
r"ca*t"
# "ct", "cat", "caat" ..
r"ca+t"
# "ct" 매치X, "cat" or "caat" or "caaa...aat"
r"ca{2,4}t"
# "c + a가2~4회반복 + t" : "caat" or "caaat" or "caaaat"
r"ab?c"
# "abc" or "ac"
import re
print(re.search('^Life', 'Life is too short'))
# <re.Match object; span=(0, 4), match='Life'>
print(re.search('^Life', 'My Life'))
# None
p = re.compile("^python\s\w+", re.M)
data = """python one
life is too short
python two
you need python
python three"""
print(p.findall(data))
# ['python one', 'python two', 'python three']
p = re.compile(r'\Bclass\B')
print(p.search('no class at all'))
# None
print(p.search('one subclass is'))
# None
print(p.search('the declassified algorithm'))
# <re.Match object; span=(6, 11), match='class'>
p = re.compile('a.b', re.DOTALL)
m = p.match('a\nb')
print(m)
# <re.Match object; span=(0, 3), match='a\nb'>