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 7988901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:35:34+00:00 2026-06-04T12:35:34+00:00

I am trying extract some information from the below given string >>> st =

  • 0

I am trying extract some information from the below given string

>>> st = '''
... <!-- info mp3 here -->
...                             192 kbps<br />2:41<br />3.71 mb  </div>
... <!-- info mp3 here -->
...                             3.49 mb  </div>
... <!-- info mp3 here -->
...                             128 kbps<br />3:31<br />3.3 mb   </div>
... '''
>>>

Now when I use the below regex my output is

>>> p = re.findall(r'<!-- info mp3 here -->\s+(.*?)<br />(.*?)<br />(.*?)\s+</div>',st)
>>> p
[('192 kbps', '2:41', '3.71 mb'), ('128 kbps', '3:31', '3.3 mb')]

but my required output is

[('192 kbps', '2:41', '3.71 mb'),(None,None,'3.49mb'), ('128 kbps', '3:31', '3.3 mb')]

So, my question is how do I change my above regex to match all the conditions.I believe my current regex is strictly dependent on <br /> tags so how do I make it conditional on that.

I know I should not be using regex to parse html but currently this is the most appropriate way for me.

  • 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-06-04T12:35:36+00:00Added an answer on June 4, 2026 at 12:35 pm

    The following will work, though I wonder if there’s not a more elegant solution. You can certainly combine the list comprehensions into one line, but I think that makes the code less clear overall. At least this way you’ll be able to follow what you did three months from now…

    st = '''
    <!-- info mp3 here -->
                                192 kbps<br />2:41<br />3.71 mb  </div>
    <!-- info mp3 here -->
                                3.49 mb  </div>
    <!-- info mp3 here -->
                                128 kbps<br />3:31<br />3.3 mb   </div>
    '''
    
    p = re.findall(r'<!-- info mp3 here -->\s+(.*?)\s+</div>',st)
    p2 = [row.split('<br />') for row in p]
    p3 = [[None]*(3 - len(row)) + row for row in p2]
    
    >>> p3
    [['192 kbps', '2:41', '3.71 mb'], [None, None, '3.49 mb'], ['128 kbps', '3:31', '3.3 mb']]
    

    And, depending on the variability in your string, you may want to write a more generic cleaning function that strips, cases, whatever, and map it to each item you pull out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml from which I am trying to extract some information through
I'm trying to write some code to extract Exif information from a JPG. Exif
I'm trying to extract some information from a webpage using php cURL+preg_match or any
I'm trying to use Selenium (in Python) to extract some information from a website.
I'm trying to extract some information from an xml file using xslt. I've used
I am trying to extract some information from a binary file. It looks like
I'm trying to extract some extra information from a location using google map api.
Hey Im trying to extract certain information from a string. The String looks like
I'm trying to extract some string from a file using python re, then MD5ing
I'm trying to extract some information from web server log and it's not very

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.