john
john

Reputation: 2662

Python re to replace the subtitles with new formats

I have a file with the following format:

===Subtitle 1===
text ....

===Subtitle 2===
text ....

How could I replace ===Subtitle 1=== with Section Subtitle 1 using python?

I tried this:

import re

s = '===Subtitle 1==='
lst = re.findall('===[\S+a-zA-Z0-9]===', s)

print lst

But I cannot print out anything.

Upvotes: 0

Views: 117

Answers (1)

Remi Guan
Remi Guan

Reputation: 22282

You don't need use regex here, just use str.replace() like this:

>>> a = '===Subtitle 1==='
>>> a.replace('=', '')
'Subtitle 1'
>>>

But if you'd like use regex...

>>> import re
>>> re.findall('=+(.+?)=+', a)
['Subtitle 1']
>>> re.findall('=+(.+?)=+', a)[0]
'Subtitle 1'
>>> 

Upvotes: 2

Related Questions