Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

python - Delete the words between [], {}, and ()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you use [.*] it does a greedy match, so everything from [a till b] is matched and substituted for the empty string ''.

When you use [.?], it matches anychar . zero or 1 time ? which are inside []. And so [a] and [b] are matched.

import re

with open('file1.txt') as file1:
    file1 = file1.read()
test = re.sub(r'[([].?[)]]', '', file1)

print(test)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...