-2

Have a string:

myString = '<p>Phone Number:</p><p>706-878-8888</p>'

Trying to regex out all HTML tags, in this case Paragraphs.

Thanks!

4

2 Answers 2

2

Using BeautifulSoup as pointed out by a comment:

>>> from BeautifulSoup import BeautifulSoup
>>> BeautifulSoup(myString).text
u'Phone Number:706-878-8888'
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I kept trying attribute 'string' instead of text. Much thanks!
2

Use re.sub:

>>> re.sub('<[^>]+>', '', '<p>Phone Number:</p><p>706-878-8888</p>')
'Phone Number:706-878-8888'

Using re is a good solution if you just want to remove tags. But, if you're want to do things a little bit more complicated (involving HTML parsing) I suggest you to look into BeautifulSoup.

Comments

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.