我的文字中的记录以PHOTO
开头,以\n
结尾,如何使用正则表达式删除此文本。
答案 0 :(得分:11)
我正在做几种方法。
切割字符串:
>>> string = "PHOTOThe text which should be present\n"
>>> string[5:-1]
'The text which should be present'
使用正则表达式:
>>> import re
>>> match = re.match("PHOTO(.+)\n", string)
>>> match.groups()
('The text which should be present',)
答案 1 :(得分:3)
我知道这不是正则表达式,但
怎么样x = x[5:-1]
答案 2 :(得分:2)
你尝试过这样的事吗?
re.sub('^PHOTO.*\n', '', <your_string>)