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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 239k
  • Answers 239k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, your table definitions are saved; Oracle DDL has an… May 13, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer As the documentation states, you can pass it initial data… May 13, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer Do you want to highlight all of the rows that… May 13, 2026 at 7:03 am

Related Questions

Edit: From another question I provided an answer that has links to a lot
How do I fix a broken debugger, one that just won't start, in EclipseME
I'm leaving out all the cursor setup and the SELECT from the temp table
std::auto_ptr is broken in VC++ 8 (which is what we use at work). My

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.