Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove tags from a string in python using regular expressions? (NOT in HTML)

I need to remove tags from a string in python.

<FNT name="Century Schoolbook" size="22">Title</FNT>

What is the most efficient way to remove the entire tag on both ends, leaving only "Title"? I've only seen ways to do this with HTML tags, and that hasn't worked for me in python. I'm using this particularly for ArcMap, a GIS program. It has it's own tags for its layout elements, and I just need to remove the tags for two specific title text elements. I believe regular expressions should work fine for this, but I'm open to any other suggestions.

like image 616
Tanner Semerad Avatar asked Sep 07 '10 19:09

Tanner Semerad


People also ask

How do I remove a string tag?

The HTML tags can be removed from a given string by using replaceAll() method of String class.


2 Answers

Please avoid using regex. Eventhough regex will work on your simple string, but you'd get problem in the future if you get a complex one.

You can use BeautifulSoup get_text() feature.

from bs4 import BeautifulSoup

text = '<FNT name="Century Schoolbook" size="22">Title</FNT>'
soup = BeautifulSoup(text)

print(soup.get_text())
like image 29
Aminah Nuraini Avatar answered Sep 20 '22 12:09

Aminah Nuraini


This should work:

import re
re.sub('<[^>]*>', '', mystring)

To everyone saying that regexes are not the correct tool for the job:

The context of the problem is such that all the objections regarding regular/context-free languages are invalid. His language essentially consists of three entities: a = <, b = >, and c = [^><]+. He wants to remove any occurrences of acb. This fairly directly characterizes his problem as one involving a context-free grammar, and it is not much harder to characterize it as a regular one.

I know everyone likes the "you can't parse HTML with regular expressions" answer, but the OP doesn't want to parse it, he just wants to perform a simple transformation.

like image 131
Domenic Avatar answered Sep 20 '22 12:09

Domenic