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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:03:07+00:00 2026-06-13T20:03:07+00:00

I saw this regular expression performed on an url: $url = ‘http://www.domain.com/’; preg_match(‘/(http)(.*?)\n/’, $url,

  • 0

I saw this regular expression performed on an url:

$url = 'http://www.domain.com/';
preg_match('/(http)(.*?)\n/', $url, $matches);

I am not sure what the use of the question mark “?” is in this regex expression. According to regex manuals, the “?” is a meta character that is equivalent to {0,1}. Then, what is the point of having “?” after an * since * already represents {0,}

Can someone please enlighten me. Thanks.

  • 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-13T20:03:08+00:00Added an answer on June 13, 2026 at 8:03 pm

    It has a different meaning when it follows another quantifier.

    In this case it changes the matching behaviour of the preceding quantifier. The default behaviour is greedy and the the ? changes it to “ungreedy”.

    • “Greedy” means match as much as possible

    • “Ungreedy” means match as less as possible

    See the article on regular-expression.info

    For example:

    a.+b will match “aabxb” in aabxb

    a.+?b will match only “aab” in aabxb

    See the example here on Regexr

    You may be interested in my blog post about this topic: You do know Quantifiers. Really?

    About your regex

    preg_match('/(http)(.*?)\n/', $url, $matches);
    

    I don’t think it makes a difference here. The . matches anything but newline characters by default (you can change this by adding a s after the closing regex delimiter), so if the question mark is there or not, it will match only till the first \n.

    If you change the behaviour by using preg_match('/(http)(.*?)\n/s', $url, $matches);, it will make a difference. .*\n would match till the last \n and .*?\n will stop at the first \n.

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

Sidebar

Related Questions

[^\x20-\x7E] I saw this pattern used for a regular expression in which the goal
I once saw a regular expression example ^\s*(+-)?(\d+|\d*\.\d+|\d+\.\d*)(e?(+-)?......$ but I just do not know
I saw this code https://gist.github.com/takuma104/ntlniph/blob/master/gtm/Foundation/GTMNSString+HTML.h but it doesn't work on ARC enabled projects or
I'm using a regular expression to replace commas that are not contained by text
I saw some other similar questions on this topic here but they were not
I saw this as an example on MDN and didn't understand why this was
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I saw this question and it motivated me to look again (without success) at
I saw this posting which explained how to get BC3 working as the diff
i saw this In MySQL, joins work for INSERT, UPDATE, and DELETE statements. It's

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.