1

My Initial String consists of <span> and some contents in between and a </span></span , I would like to remove that piece(including span and contents inside it and /span) from my string , what should I do ?

Part of String that need to be Removed : "<span class="_5mfr"><span class="_6qdm" style='height: 16px; width: 16px; font-size: 16px; background-image: url("https://static.xx.fbcdn.net/images/emoji.php/v9/t81/1/16/")+14 variable strings+</span></span

I would like to remove that whole piece mentioned above

5
  • what have you tried so far? Commented Sep 17, 2021 at 5:48
  • The quick way would be s = s.replace('<span>','').replace('</span>',''). Commented Sep 17, 2021 at 5:49
  • @TimRoberts It Didn't Worked :(,it returned the same old string Commented Sep 17, 2021 at 5:52
  • @TimRoberts, no > in the </span Commented Sep 17, 2021 at 5:55
  • @PCM yeah by that most part of the span thing removed,but some part is till there like mentioned below Commented Sep 17, 2021 at 5:58

3 Answers 3

2
import re

txt = 'Iam a good boy <span>some blahblahblah </span</span and my name is john'
print(re.sub(r'<span>.*</span</span ', '', txt))

Prints:

Iam a good boy and my name is john

to the updated question

import re

txt = """<span class="_5mfr"><span class="_6qdm" style='height: 16px; width: 16px; font-size: 16px; background-image: url("https://static.xx.fbcdn.net/images/emoji.php/v9/t81/1/16/")+14 variable strings+</span></span"""
print(re.sub(r'<span [^<>]*?</span>?</span', '', txt))
# prints: <span class="_5mfr">
Sign up to request clarification or add additional context in comments.

3 Comments

Did this work with the string you updated in your question? I don't think the accepted answer solves for the current string.
Yeah,This Didn't Worked,I was using Except block with the program and I thought that problem had solved
1

Use BeautifulSoup:

from bs4 import BeautifulSoup
soup = BeautifulSoup(string, 'html.parser')
for x in soup.findAll('span'):
    x.replace_with('')
print(soup.string)

10 Comments

It's not by me,Bro It's somebody Else
@SimpleGuy_ Ah! yeap! Hope it works for you!
Anyway,I've changed my question a little bit so that my aim is clear to everyone
@SimpleGuy_ Added working code, this is much more consistent
@SimpleGuy_ Great!
|
0

You can replace everything found by the regex as shown below:

import re

regex = r"(<span.+?>)|(<\/span>)"

test_str = "<span class=\\\"_5mfr\\\"><span class=\\\"_6qdm\\\" style='height: 16px; width: 16px; font-size: 16px; background-image: url(\\\"static.xx.fbcdn.net/images/emoji.php/v9/t81/1/16/…\\\")'>© Dasamoolam Damu (Troll Malayalam)ഹൗ ക്രൂവൽ<span class=\\\"_5mfr\\\"><span class=\\\"_6qdm\\\" style='height: 16px; width: 16px; font-size: 16px; background-image: url(\\\"static.xx.fbcdn.net/images/emoji.php/v9/td7/1/16/…\\\")'></span></span></span></span>"

print(re.sub(regex, '', test_str))

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.