60 lines
1.4 KiB
Python
Executable File
60 lines
1.4 KiB
Python
Executable File
#!/usr/bin/python
|
|
|
|
import re
|
|
|
|
|
|
def fn_match():
|
|
# 문자열의 전체가 정규식과 일치하는지 판별합니다.
|
|
# 일치할 때에는 Match 객체를, 일치하지 않으면 None을 돌려줍니다.
|
|
str = "bat"
|
|
|
|
pattern = re.compile("[a-z]at")
|
|
match = pattern.match(str)
|
|
if match:
|
|
print("Matches: ", match.group())
|
|
else:
|
|
print("Not matches.")
|
|
|
|
|
|
def fn_search():
|
|
# 문자열 중에서 정규식과 일치하는 부분을 찾습니다.
|
|
# 일치할 때에는 Match 객체를, 일치하지 않으면 None을 돌려줍니다.
|
|
str = "I'm the bat man. Where is the rat?"
|
|
|
|
pattern = re.compile("[a-z]at")
|
|
match = pattern.search(str)
|
|
if match:
|
|
print("Matches: ", match.group())
|
|
else:
|
|
print("Not matches.")
|
|
|
|
|
|
def fn_findall():
|
|
# 정규식과 일치되는 모든 문자열을 리스트로 반환합니다.
|
|
str = "I'm the bat man. Where is the rat?"
|
|
|
|
pattern = re.compile("[a-z]at")
|
|
list = pattern.findall(str)
|
|
print(list)
|
|
|
|
|
|
def main():
|
|
fn_match()
|
|
fn_search()
|
|
fn_findall()
|
|
|
|
|
|
def test_1():
|
|
line = '\tid("com.github.ben-manes.versions") version "0.42.0"'
|
|
pattern = re.compile(
|
|
'id\("com.github.ben-manes.versions"\) version "([0-9.]+)"')
|
|
match = pattern.search(line)
|
|
if match:
|
|
line = line.replace(match.group(1), "0.46.0")
|
|
print(line)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# main()
|
|
test_1()
|