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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:16:48+00:00 2026-05-18T21:16:48+00:00

I’ve set myself a somewhat ambitious first task in learning regular expressions (and one

  • 0

I’ve set myself a somewhat ambitious first task in learning regular expressions (and one which relates to a problem I’m trying to solve). I need to find any instance of a url that ends in .m4v, in a big html string.

My first attempt was this for jpg files

http.*jpg

Which of course seems correct on first glance, but of course returns stuff like this:

http://domain.com/page.html" title="Misc"><img src="http://domain.com/image.jpg

Which does match the expression in theory. So really, I need to put something in http.*m4v that says ‘only the closest instance between http and m4v’. Any ideas?

  • 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-18T21:16:48+00:00Added an answer on May 18, 2026 at 9:16 pm

    As you’ve noticed, an expression such as the following is greedy:

    http:.*\.jpg
    

    That means it reads as much input as possible while satisfying the expression.

    It’s the “*” operator that makes it greedy. There’s a well-defined regex technique to making this non-greedy… use the “?” modifier after the “*“.

    http:.*?\.jpg
    

    Now it will match as little as possible while still satisifying the expression (i.e. it will stop searching at the first occurrence of “.jpg”.

    Of course, if you have a .jpg in the middle of a URL, like:

    http://mydomain.com/some.jpg-folder/foo.jpg
    

    It will not match the full URL.

    You’ll want to define the end of the URL as something that can’t be considered part of the URL, such as a space, or a new line, or (if the URL in nested inside parentheses), a closing parenthesis. This can’t be solved with just one little regex however if it’s included in written language, since URLs are often ambiguous.

    Take for example:

    At this page, http://mysite.com/puppy.html, there's a cute little puppy dog.
    

    The comma could technically be a part of a URL. You have to deal with a lot of ambiguities like this when looking for URLs in written text, and it’s hard not to have bugs due to the ambiguities.

    EDIT | Here’s an example of a regex in PHP that is a quick and dirty solution, being greedy only where needed and trying to deal with the English language:

    <?php
    
    $str = "Checkout http://www.foo.com/test?items=bat,ball, for info about bats and balls";
    
    preg_match('/https?:\/\/([a-zA-Z0-9][a-zA-Z0-9-]*)(\.[a-zA-Z0-9-]+)*((\/[^\s]*)(?=[\s\.,;!\?]))\b/i', $str, $matches);
    
    var_dump($matches);
    

    It outputs:

    array(5) {
      [0]=>
      string(38) "http://www.foo.com/test?items=bat,ball"
      [1]=>
      string(3) "www"
      [2]=>
      string(4) ".com"
      [3]=>
      string(20) "/test?items=bat,ball"
      [4]=>
      string(20) "/test?items=bat,ball"
    }
    

    The explanation is in the comments.

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

Sidebar

Related Questions

No related questions found

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.