| Operators are special tokens that represent computations like addition, multiplication and division. |
| + : Addition – : Subtraction * : Multiplication / : Division 18 / 4 # 4.5 ** : Exponentiation // : truncated division 9 // 5 # 1 7.0 // 3.0 # 2.0 % : (Integer) Remainder 7 % 3 # 1 |
|
print( ‘a’ * 10 ) # aaaaaaaaaa
not True, not False name = “Chulsung”
age = 10 var1 = name + ‘ ‘ + str(age) 1] The addition, subtraction, multification, division: +, -, *, /
2] The power: **
print( 10*2 ) # 1003] The remainder (나머지): % print( 5%2 + 10%2) # 1 + 0 4] the quotreut (몫) : //
print( 5//2 + 10//2 ) # 2 + 5 5] != , not( )
|
|
import re
data = “””
park 800905-1049118
kim 700905-1059119
“””
pat = re.compile(r”(\d{6})[-]\d{7}”)
print(pat.sub(r”\g<1>-*******”, data))
|
| park 800905-******* kim 700905-******* |
|
[abc] : 문자들과 매치
[a-c] a,b, c
[1-5]
|
|
Dot(.)
모든 한 문자와 한 숫자
|
|
반복(*) 0번 이상 반복
(+)
ca*t
cat, caat
반복({m, n})
ca{3,4}t
caat, caaaaat
반복 ?
ab?c
abc, ac
|
|
p = re.compile(‘[a-z]+’)
m = p.match(‘python’)
|
|
p.search(“python”)
p.findall(“life is too short”)
p.finditer(“life is too short”)
|
|
import re
p = re.compile(‘[a-z]+’)
m = p.match(“python”)
print(m.group()) # python
print(m.start()) # 0
print(m.end()) # 6
print(m.span()) # (0,6)
|
|
import re
p = re.compile(‘[a-z]+’)
m = p.search(“3 python”)
print(m.group())
print(m.start()) # 2
print(m.end()) # 8
print(m.span()) # (2, 8)
Compile Options
p = re.compile(‘a.b’, re.DOTALL)
p = re.compile(‘[a-.z]+’, re..)
p = re.compile(‘^pthon\s\w+’, re.MULTILINE)
p = re.compile(‘[…]’, re.VERBOSE)
p = re.compile(r’\\section’)
^: 맨 처음과 일치
$: 맨 끝과 일치
\A: 전체 물자열의 맨 처음과 일치
\Z:
r’\b….\b’
r’\B…\B’ 공백이 없는 것
‘(ABC)+’
\w : 문자 + 숫자
\s whitespace
\d : 숫자
(\w+)
m.group(n)
import re
p = re.compile(‘.+:’)
m = p.match(” http://google.com”)
print(m.group()) # http:
p = re.compile(‘.+(?=:)’)
m = p.match(” http://google.com”)
print(m.group()) # http p = re.compile(“.*[.](?!bat$).*$”, re.M)
m = p.findall(“””
autoexec.exe
autoexec.bat
autoexec.jpg
“””)
print(m) # [‘autoexec.exe’, ‘autoexec.jpg’] def hexrepl(match):
value = int(match.group())
return hex(value)
p = re.compile(r’\d+’)
print(p.sub(hexrepl, ‘Call 1234 for printing, 2367 for user …’))
# Call 0x4d2 for printing, 0x93f for user …
s = ‘<html><head><title>Title</title>’
print(re.match(‘<.*>’, s).span()) # (0, 32)
print(re.match(‘<.*>’, s).group()) # <html><head><title>Title</title>
|
Posted by