103

I already extract some information from a forum. It is the raw string I have now:

string = 'i think mabe 124 + <font color="black"><font face="Times New Roman">but I don\'t have a big experience it just how I see it in my eyes <font color="green"><font face="Arial">fun stuff'

The thing I do not like is the sub string "<font color="black"><font face="Times New Roman">" and "<font color="green"><font face="Arial">". I do want to keep the other part of string except this. So the result should be like this

resultString = "i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff"

How could I do this? Actually I used beautiful soup to extract the string above from a forum. Now I may prefer regular expression to remove the part.

4
  • this string is currently not working, it has both " and ' inside Commented Jan 2, 2012 at 16:23
  • @ThiefMaster Thanks for support. How could I remove it? It IS a shame for sure Commented Jan 2, 2012 at 16:23
  • @julio.alegria Please just treat the thing between beginning " and ending " as a string if you wanna some test. thanks lot Commented Jan 2, 2012 at 16:24
  • 2
    I dont get it, you extract the text with beautifulsoup, but you want to stop using it before you're done because ... ? Commented Jan 2, 2012 at 16:36

3 Answers 3

192
import re
re.sub('<.*?>', '', string)
"i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff"

The re.sub function takes a regular expresion and replace all the matches in the string with the second parameter. In this case, we are searching for all tags ('<.*?>') and replacing them with nothing ('').

The ? is used in re for non-greedy searches.

More about the re module.

Sign up to request clarification or add additional context in comments.

2 Comments

this is very helpful.. thanks . I used this to remove mentions (@s) in twitter tweets for my project - re.sub('@.*? ', '', tweetText)
I need to remove patterns like size 6.5 from mens tommy hilfiger knot boatshoe midnight uk size 6.5. If I use re.sub('size.*?[0-9]+', '', shoe), I get mens tommy hilfiger knot boatshoe midnight uk .5
21
>>> import re
>>> st = " i think mabe 124 + <font color=\"black\"><font face=\"Times New Roman\">but I don't have a big experience it just how I see it in my eyes <font color=\"green\"><font face=\"Arial\">fun stuff"
>>> re.sub("<.*?>","",st)
" i think mabe 124 + but I don't have a big experience it just how I see it in my eyes fun stuff"
>>> 

Comments

-7
BeautifulSoup(text, features="html.parser").text 

For the people who were seeking deep info in my answer, sorry.

I'll explain it.

Beautifulsoup is a widely use python package that helps the user (developer) to interact with HTML within python.

The above like just take all the HTML text (text) and cast it to Beautifulsoup object - that means behind the sense its parses everything up (Every HTML tag within the given text)

Once done so, we just request all the text from within the HTML object.

2 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes.
Hey Sorry, sometimes I feel the question is so straightforward that the real answer is the actual implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.