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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:31:53+00:00 2026-05-12T08:31:53+00:00

An answer from another question piqued my curiosity. Consider: $string = asfasdfasdfasdfasdf[[sometextomatch]]asfkjasdfjaskldfj; $regex =

  • 0

An answer from another question piqued my curiosity.

Consider:

$string = "asfasdfasdfasdfasdf[[sometextomatch]]asfkjasdfjaskldfj";

$regex = "/\[\[(.+?)\]\]/";
preg_match($regex, $string, $matches);

$regex = "/\[\[(.*)\]\]/"; 
preg_match($regex, $string, $matches);

I asked what the difference between the two regexes is. The aswer I got was that “.*” matches any character 0 or more times as many times as possible,
and “.+?” matches any character 1 or more times as few times as possible.

I read those regexes differently so I did some experimenting on my own but didn’t come to any conclusions. Php.net says “?” is equivalent to {0,1} so you could rewrite

"/\[\[(.+?)\]\]/"

as

"/\[\[((.+){0,1})\]\]/"

or as

"/\[\[(.{0,})\]\]/"

or as

"/\[\[(.*)\]\]/"

Will they capture different text? Is the difference that one is less expensive? Am I being anal?

  • 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-12T08:31:53+00:00Added an answer on May 12, 2026 at 8:31 am

    Just take an example where you get different results:

    foo [[bar]] baz [[quux]]
    

    Your first regular expression will match [[bar]] and [[quux]] while the second will match only [[bar]] baz [[quux]].

    The reason for that is that a lazy quantifier (suffixed with ?) will match the minimum of possible repetitions the normal greedy mode will match the maximum of possible repetitions:

    However, if a quantifier is followed by a question mark, then it ceases to be greedy, and instead matches the minimum number of times possible, so the pattern /\*.*?\*/ does the right thing with the C comments. The meaning of the various quantifiers is not otherwise changed, just the preferred number of matches. Do not confuse this use of question mark with its use as a quantifier in its own right. Because it has two uses, it can sometimes appear doubled, as in \d??\d which matches one digit by preference, but can match two if that is the only way the rest of the pattern matches.

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

Sidebar

Related Questions

Per a great answer from another question I have begun mounting global resources (css/js/images)
Edit: From another question I provided an answer that has links to a lot
this question is based on answer got from another SO question, can be found
This is specifically related to the accepted answer of another question I asked .
This relates to another question I asked earlier today. I built SVN 1.6.2 from
I was able to get part of my answer from another question but cannot
After getting the right answer from this question , I am having another problem
I found a find-and-replace text answer from another question. How do I use PowerShell
From another answer on Stackoverflow is a conversion from Javascript date to .net DateTime:
This is a CSS related question, I got one good answer from my previous

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.