Skip to content Skip to sidebar Skip to footer

How Do Capture Groups Work? (wrt Python Regular Expressions)

While using regex to help solve a problem in the Python Challenge, I came across some behaviour that confused me. from here: (...) Matches whatever regular expression is inside the

Solution 1:

Because you only have one capturing group, but it's "run" repeatedly, the new matches are repeatedly entered into the "storage space" for that group. In other words, the 1s were lost when they were "overwritten" by subsequent 1s and eventually the 2.

Solution 2:

You are repeating the group itself by appending '+' after ')', I do not know the implementation details but it matches 7 times, and returns only the last match.

In the first one, you are matching 7 digits, and making it a group.

Post a Comment for "How Do Capture Groups Work? (wrt Python Regular Expressions)"