Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6692859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:54:58+00:00 2026-05-26T05:54:58+00:00

I am getting the first paragraph from pages and trying to extract words suitable

  • 0

I am getting the first paragraph from pages and trying to extract words suitable to be tags or keywords. In some paragraphs there are links and I want to remove the tags:

For instance if the text is

A <b>hex triplet</b> is a six-digit, three-<a href="/wiki/Byte"
enter code heretitle="Byte">byte</a> ...

I want to remove

<b></b><a href="/wiki/Byte" title="Byte"></a>

to end up with this

A hex triplet is a six-digit, three-byte ...

A regex like this does not work:

>>> text = """A <b>hex triplet</b> is a six-digit, three-<a href="/wiki/Byte"
    enter code heretitle="Byte">byte</a> ..."""
>>> f = re.findall(r'<.+>', text)
>>> f
['<b>hex triplet</b>', '</a>']
>>>

What is the best way to do this?

I found several similar questions but none of them I think solves this particular problem.

Update with an example of BeautifulSoup extract (extract deletes the tag including its text and must run for each tag separately:

>>> soup = BeautifulSoup(text)
>>> [s.extract() for s in soup('b')]
[<b>hex triplet</b>]
>>> soup
A  is a six-digit, three-<a href="/wiki/Byte" enter code heretitle="Byte">byte</a> ...
>>> [s.extract() for s in soup('a')]
[<a href="/wiki/Byte" enter code heretitle="Byte">byte</a>]
>>> soup
A  is a six-digit, three- ...
>>> 

Update

For people with the same question: as mentioned by Brendan Long, this answer using HtmlParser works best.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:54:58+00:00Added an answer on May 26, 2026 at 5:54 am

    The + quantifier is greedy, meaning it will find the longest possible match. Add a ? to force it to find the shortest possible match:

    >>> re.findall(r'<.+?>', text)
    ['<b>', '</b>', '</a>']
    

    Another way to write the regex is to explicitly exclude right angle brackets inside a tag, using [^>] instead of ..

    >>> re.findall(r'<[^>]+>', text)
    ['<b>', '</b>', '<a href="/wiki/Byte"\n    enter code heretitle="Byte">', '</a>']
    

    An advantage of this approach is that it will also match newlines (\n). You can get the same behavior with . if you add the re.DOTALL flag.

    >>> re.findall(r'<.+?>', text, re.DOTALL)
    ['<b>', '</b>', '<a href="/wiki/Byte"\n    enter code heretitle="Byte">', '</a>']
    

    To strip out the tags, use re.sub:

    >>> re.sub(r'<.+?>', '', text, flags=re.DOTALL)
    'A hex triplet is a six-digit, three-byte ...'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting the subdomain from a URL sounds easy at first. http://www.domain.example Scan for the
I am trying to first getting the value of a div, then adding character
I'm trying to implement an outdent of the first letter of the first paragraph
I'm running this query: select * from schema.table but i'm only getting the first
I am trying to get the cells from 1st column in a table. Getting
Reference to pre-answered question at: Getting first and last days of current week There
I am currently getting first day Of this week and last week values with
I'm getting ready to develop my first Silverlight app. It is going to be
I'm getting a segmentation fault the first time I call malloc() after I protect
I'm getting ready to finally deploy my first iPhone app. The app uses SSL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.