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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:34:36+00:00 2026-06-05T23:34:36+00:00

I found there’re two different opinions about how greedy regex is executed: one is,

  • 0

I found there’re two different opinions about how greedy regex is executed:

  • one is, read all the input string and match the pattern from the back, first match entire input,the first attempt is entire string. Some articles support this opinion are Oracle offical Java tutorial:

Greedy quantifiers are considered “greedy” because they force the
matcher to read in, or eat, the entire input string prior to
attempting the first match. If the first match attempt (the entire
input string) fails, the matcher backs off the input string by one
character and tries again, repeating the process until a match is
found or there are no more characters left to back off from.

also see this article: Performance of Greedy vs. Lazy Regex Quantifiers

  • the other is matching from the front, the first match attempt is from the 0 index in the left. when a match is found, the engine doesn’t stop, keep matching the rest until it fails then it’ll backtrack. Articles supports this opinion I found is:

Repetition with Star and Plus the Looking Inside The Regex Engine section talk about <.+>:

The first token in the regex is <. This is a literal. As we already
know, the first place where it will match is the first < in the
string.

I want to know which one is correct? This matters because it will affect the efficiency of regex. I added various language tags, because I want to know if it’s implemented differently in each language.

  • 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-05T23:34:37+00:00Added an answer on June 5, 2026 at 11:34 pm

    “The matcher backs off the input string by one character and tries again” is just describing backtracking, so “then it’ll backtrack” is therefore saying the same thing. Since both of your quotes regarding greediness say the same thing, both are correct. (Your third quote has nothing to do with greediness.)


    Let’s provide an example.

    'xxabbbbbxxabbbbbbbbb' =~ /([ab]*)bb/;
    
    1. Try at pos 0.
      1. [ab]* matches 0 chars “”.
        1. At pos 0, bb fails to match ⇒ Backtrack.
      2. [ab]* can’t match any less ⇒ Backtrack.
    2. Try at pos 1.
      1. [ab]* matches 0 chars “”.
        1. At pos 1, bb fails to match ⇒ Backtrack.
      2. [ab]* can’t match any less ⇒ Backtrack.
    3. Try at pos 2.
      1. [ab]* matches 6 chars “abbbbb“.
        1. At pos 8, bb fails to match ⇒ Backtrack.
      2. [ab]* matches 5 chars “abbbb“. (Back off one)
        1. At pos 7, bb fails to match ⇒ Backtrack.
      3. [ab]* matches 4 chars “abbb“. (Back off one)
        1. At pos 6, bb matches.
          1. Success.

    So $1 is “abbb”. (Not abbbbbbb. “Greedy” doesn’t mean “longest match possible”.)


    Now, let’s see what happens if we make the “*” non-greedy.

    'xxabbbbbxxabbbbbbbbb' =~ /([ab]*?)bb/;
    
    1. Try at pos 0.
      1. [ab]*? matches 0 chars “”.
        1. At pos 0, bb fails to match ⇒ Backtrack.
      2. [ab]* can’t match any more ⇒ Backtrack.
    2. Try at pos 1.
      1. [ab]*? matches 0 chars “”.
        1. At pos 1, bb fails to match ⇒ Backtrack.
      2. [ab]* can’t match any more ⇒ Backtrack.
    3. Try at pos 2.
      1. [ab]*? matches 0 chars “”.
        1. At pos 2, bb fails to match ⇒ Backtrack.
      2. [ab]*? matches 1 char “a“. (Add one)
        1. At pos 3, bb matches.
          1. Success.

    So $1 is “a”.


    Specific implementation might do things differently as an optimisation, as long as it gives the same result as presented here. You can see Perl at work using

    perl -Mre=debug -E'say "xxabbbbbxxabbbbbbbbb" =~ /([ab]*)bb/;'
    perl -Mre=debug -E'say "xxabbbbbxxabbbbbbbbb" =~ /([ab]*?)bb/;'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read the Intel manual and found there is a lock prefix for instructions,
I was refactoring some code and found there are two places that can be
After running exec sp_lock on one of our databases we found there's close to
is there anyone have read the source code of dtl in c++? I found
I found there are two ways to approach it, ReadDirectoryChangesW and FindFirstChangeNotification . I
I found there are some special chars ',%.... not compatible to sqlite enquiry string.
I've opened winnt.h header file and found there this two lines: typedef wchar_t WCHAR;
I read an article here and I used the principles found there to write
I found there are two ways (submit and execute) to add a Runnable into
I read the apple documentation and some other links and found there are examples

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.