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

asked Sep 17, 2021 at 5:46
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">
answered Sep 17, 2021 at 6:00
3
  • Did this work with the string you updated in your question? I don't think the accepted answer solves for the current string. Commented Sep 17, 2021 at 6:26
  • Yeah,This Didn't Worked,I was using Except block with the program and I thought that problem had solved Commented Sep 17, 2021 at 6:36
  • Did you try this one stackoverflow.com/a/69218568/449378 ? Commented Sep 17, 2021 at 7:59
1

Use BeautifulSoup:

from bs4 import BeautifulSoup
soup = BeautifulSoup(string, 'html.parser')
for x in soup.findAll('span'):
 x.replace_with('')
print(soup.string)
answered Sep 17, 2021 at 6:02
10
  • 1
    It's not by me,Bro It's somebody Else Commented Sep 17, 2021 at 6:06
  • @SimpleGuy_ Ah! yeap! Hope it works for you! Commented Sep 17, 2021 at 6:07
  • Anyway,I've changed my question a little bit so that my aim is clear to everyone Commented Sep 17, 2021 at 6:07
  • @SimpleGuy_ Added working code, this is much more consistent Commented Sep 17, 2021 at 6:17
  • @SimpleGuy_ Great! Commented Sep 17, 2021 at 6:19
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))
answered Sep 17, 2021 at 6:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.