Why does (.*)* make two matches and select nothing in group $1?

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"


  1. Maximo

    • 2019/5/4

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

  2. Pagano

    • 2017/2/27

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

  3. Clayton

    • 2019/5/24

    The number of records in the source. Single Source Masters, Highest ranking member of a match group whose members all came from the same source. Can be from 

  4. Lincoln

    • 2020/12/20

    So, `\1' matches `ab' and `\2' matches `a'. If the group doesn't participate in a match, i.e., it is part of an alternative not taken or a repetition operator allows zero repetitions of it, then the back reference makes the whole match fail. For example, `(one()|two())-and-(three\2|four\3)' matches `one-and-three' and `two-and-four', but not `one-and-four' or `two-and-three'. For example, if the pattern matches `one-and-', then its group 2

  5. Ira

    • 2015/2/24

    Move 2 matches to new positions to get only 4 16 Squares to none - Solution. 11. Page 6. 3 squares to 5. Move 6 matches so that 5 squares are formed.

  6. Jeffrey

    • 2018/8/25

    In SQL Server you can only select columns that are part of the GROUP BY clause, or aggregate functions on any of the other columns. I've blogged about this in detail here. So you have two options: Add the additional columns to the GROUP BY clause: GROUP BY Rls.RoleName, Pro.[FirstName], Pro.[LastName] Add some aggregate function on the relevant

  7. Merrick

    • 2017/9/13

    A new dedicated group of referees, Select Group 2, will also be of the continued development of match officials, PGMOL is pleased to 

  8. Jared

    • 2019/5/24

    Here are two common queries that just don’t work: select * from table where column = null; select * from table where column <> null; They both return no rows! Countless SQL veterans have tried to explain this one to beginners. The beginner usually thinks the first row should return rows where c1 is NULL.

  9. Fernando

    • 2018/5/2

    As you can see, I have a group there that I want to extract (select in editor). CTRL + SHIFT + L selects whole match. Is there a way to select just the 

  10. Ryland

    • 2019/6/27

    1. a local area network, i.e., a network whose components are close enough to be connected without having to turn to an outside link. 2. to control or exchange data with something or someone. 3. a general term whose precise meaning depends on what the architect had in mind when designing the network.

  11. Ahmir

    • 2021/2/13

    Make sure you collect and sort out your fuel before you begin to light your fire. Select an area for your campfire. Check there are no obvious tree roots you 

  12. Damon

    • 2020/10/26

    chapter 1 quiz. 30 terms. camarokocher. unit 5 pycho. 33 terms. freeb128. AP Psych Unit 14 - Social Psychology. 90 terms. Samira_Samadi.

  13. Bianco

    • 2019/11/6

    Gerald Hawkes of Baltimore, Md., was on one of his junk hunts. 19 matches side by side make a square, that Diamond matches. measure 2 

  14. Berisha

    • 2016/3/22

    Statistics is the science of collecting, organizing, summarizing, and analyzing information to draw a conclusion and answer questions. In addition, statistics is about providing a measure of confidence in any conclusions. A (n) ____ is a person or object that is a member of the population being studied. Individual.

  15. Campbell

    • 2016/5/31

    Product details · Strike On Box Matches: You'll be ready to light the fire with these wooden matches. · Going Green: Each match is made from responsibly managed 

  16. Carl

    • 2015/3/6

    How can I get the named groups via select-string? PS > [regex]::match($text,'aa(?<digit>[\d]+)aa') | foreach {$_.groups["digit"].value}

Comments are closed.

Recent Posts