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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:25:45+00:00 2026-05-19T22:25:45+00:00

i am working on a regex match function in python. i have the following

  • 0

i am working on a regex match function in python. i have the following code:

def src_match(line, img):
    imgmatch = re.search(r'<img src="(?P<img>.*?)"', line)

    if imgmatch and imgmatch.groupdict()['img'] == img:
        print 'the match was:', imgmatch.groupdict()['img']

the above does not seem to operate correctly for me at all. i do on the other hand have luck with this:

def href_match(line, url):
    hrefmatch = re.search(r'<a href="(?P<url>.*?)"', line)

    if hrefmatch and hrefmatch.groupdict()['url'] == url:
        print 'the match was:', hrefmatch.groupdict()['url']
    else:
        return None

can someone please explain why this would be (or if maybe it seems like both should work)? for ex., is there something special about the identifier in the href_match() function?
it can be assumed in both functions that i am passing both a line in that contains the string i am searching for, and the string itself.

EDIT:
i should mention that i am sure i will never get a tag like:

<img width="200px" src="somefile.jpg"> 

the reason for this is that i am using a specific program which is generating the html and it will never yield a tag as such. this example should be taken as purely theoretical within the assumptions that i am always going to get a tag like:

<img src="somefile.jpg">

EDIT:

here is an example of a line that i am feeding to the function which does not match the input argument:

<p class="p1"><img src="myfile.anotherword.png" alt="beat-divisions.tiff"></p>
  • 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-19T22:25:46+00:00Added an answer on May 19, 2026 at 10:25 pm

    Rule #37: do not attempt parsing HTML with regex.

    Use the right tool for the job – in this case, BeautifulSoup.

    Edit:

    cut-and-pasting the function and testing as

    >>> src_match('this is <img src="my example" />','my example')
    the match was: my example
    

    so it appears to function; however it will fail on (perfectly valid) HTML code like

    <img width="200px" src="Y U NO C ME!!" />
    

    Edit4:

    >>> src_match('<p class="p1"><img src="myfile.png" alt="beat-divisions.tiff"></p>','myfile.png')
    the match was: myfile.png
    >>> src_match('<p class="p1"><img src="myfile.anotherword.png" alt="beat-divisions.tiff"</p>\n','myfile.anotherword.png')
    the match was: myfile.anotherword.png
    

    still works; are you sure the url value you are trying to match against is correct?

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

Sidebar

Related Questions

I want to match any string between [ and ]. Following code working fine
I have a RegEx that is working for me but I don't know WHY
I have a regex in c# that i'm using to match image tags and
I am working on this yahoo pipe Regex and I found a bug I'm
Working on a project at the moment and we have to implement soft deletion
Working with python interactively, it's sometimes necessary to display a result which is some
I'm trying to match the following items in the string pcode : u followed
I'm new to python programming. I have this problem: I have a list of
I have been working on my date regular expression all day... I want a
how do I get the regex mentioned in this article working with preg_match in

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.