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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:24:51+00:00 2026-06-01T19:24:51+00:00

I have a string and I want to match something at the start and

  • 0

I have a string and I want to match something at the start and end with a single search pattern. How can this be done?

Let’s say we have a string like:

 string = "ftp://www.somewhere.com/over/the/rainbow/image.jpg"

I want to do something like this:

 re.search("^ftp:// & .jpg$" ,string)

Obviously, it’s incorrect, but I hope it gets my point across. Is this possible?

  • 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-01T19:24:52+00:00Added an answer on June 1, 2026 at 7:24 pm

    re.match will match the string at the beginning, in contrast to re.search:

    re.match(r'(ftp|http)://.*\.(jpg|png)$', s)
    

    Two things to note here:

    • r'' is used for the string literal to make it trivial to have backslashes inside the regex
    • string is a standard module, so I chose s as a variable
    • If you use a regex more than once, you can use r = re.compile(...) to built the state machine once and then use r.match(s) afterwards to match the strings

    If you want, you can also use the urlparse module to parse the URL for you (though you still need to extract the extension):

    >>> allowed_schemes = ('http', 'ftp')
    >>> allowed_exts = ('png', 'jpg')
    >>> from urlparse import urlparse
    >>> url = urlparse("ftp://www.somewhere.com/over/the/rainbow/image.jpg")
    >>> url.scheme in allowed_schemes
    True
    >>> url.path.rsplit('.', 1)[1] in allowed_exts
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string such as: #sometag-{serialized-data-here} And I want to match this pattern
Hi I'm new to regexes. I have a string that I want to match
If you have a string with special characters that you want to match with:
I have a string in PHP and I want it to match the regex
I have string looking like this: 'Toy Story..(II) (1995)' I want to split the
If I have a large string with multiple lines and I want to match
Using XSLT and XPath 1.0, I have a string I want to escape for
I have a string pc1|pc2|pc3| I want to get each word on different line
I have a HTML string and want to replace all links to just a
I have a string and I want to check if it represents a proper

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.