Computer >> Máy Tính >  >> Lập trình >> Python

Biểu thức chính quy Python nào có thể được sử dụng thay cho string.replace?

Đoạn mã sau thay thế tất cả các ký tự từ chuỗi đã cho bằng ''

Ví dụ

import re
line = 'this is a text with<[2> in between</[3> and then there are instances ... where the<[43> number ranges from 0-99</[76>.\
and there are many other lines in the text files \
with<[7> such tags </[8>'
line = re.sub(r"</?\[\d+>", "", line)
print line

Đầu ra

Điều này cho kết quả

this is a text with in between and then there are instances ... where the number ranges from 0-99.and there are many other lines in the text files with such tags