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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:25:14+00:00 2026-06-17T19:25:14+00:00

This arose from a discussion on formalizing regular expressions syntax. I’ve seen this behavior

  • 0

This arose from a discussion on formalizing regular expressions syntax. I’ve seen this behavior with several regular expression parsers, hence I tagged it language-agnostic.

Take the following expression (adjust it for your favorite language):

replace("input", "(.*)*", "$1")

it will return an empty string. Why?

More curiously even, the expression replace("input", "(.*)*", "A$1B") will return the string ABAB. Why the double empty match?

Disclaimer: I know about backtracking and greedy matches, but the rules laid out by Jeffrey Friedl seem to dictate that .* matches everything and that no further backtracking or matching is done. Then why is $1 empty?

Note: compare with (.+)*, which returns the input string. However, http://regexhero.com shows that there are still two matches, which seems odd for the same reasons as above.

  • 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-17T19:25:16+00:00Added an answer on June 17, 2026 at 7:25 pm

    Let’s see what happens:

    1. (.*) matches "input".
    2. "input" is captured into group 1.
    3. The regex engine is now positioned at the end of the string. But since (.*) is repeated, another match attempt is made:
    4. (.*) matches the empty string after "input".
    5. The empty string is captured into group 1, overwriting "input".
    6. $1 now contains the empty string.

    A good question from the comments:

    Then why does replace("input", "(input)*", "A$1B") return "AinputBAB"?

    1. (input)* matches "input". It is replaced by "AinputB".
    2. (input)* matches the empty string. It is replaced by "AB" ($1 is empty because it didn’t participate in the match).
    3. Result: "AinputBAB"
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question arose from the discussion in the comments of this answer . First,
This question arose from being unable to use uniform-initialisation syntax with the auto keyword
This problem arose when trying to compile a sample program from Stroustrup's Programming Principles
This arose from a question earlier today on the subject of bignum libraries and
This situation arose when I tried to upgrade Spring from version 2.1 to Spring
I have some confusion with this that arose from messing around with exporting generic
Earlier today I asked this question which arose from A- My poor planning and
I am learning Fluent NHibernate and this issue arose from that project. I have
Note: this appears to have been fixed in Roslyn This question arose when writing
edit: This is an Android project. My problem arose when I added a new

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.