简体   繁体   中英

How would I remove the contents of these tags from a string in python?

I have this string:

[quote="Calvin_Thrasher, post:14, topic:84114, full:true"]\\nWell then why would it be in there?\\n[/quote]\\n\\nit sounds like an educational product intended to be used in a classroom setting, but not in competition.

Using python, I want to extract only

it sounds like an educational product intended to be used in a classroom setting, but not in competition.

from that. I want to get rid of everything in between [quote] and [/quote], along with the newline tags (preferably replacing them with an actual new line).

How would I go about doing that?

For that, you can use python regex module as below:

import re

my_str = """[quote="Calvin_Thrasher, post:14, topic:84114, full:true"]\nWell then why would it be in there?\n[/quote]\n\nit sounds like an educational product intended to be used in a classroom setting, but not in competition."""

new_str = re.sub('\[quote.*\[/quote\]', "", my_str, flags=re.DOTALL).strip()

Ouput new_str is: it sounds like an educational product intended to be used in a classroom setting, but not in competition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM