how does the regex like ".*" work in perl

.* matches two times. First, it matches "scott". Then it matches the empty string after "scott".


The first time through, .* matches 5 characters starting at position zero (scott). This gets replaced with /.

/g means match as many times as possible, so it tries again, this time start at position five.

The second time through, .* matches 0 characters starting at position five (empty string). This gets replaced with /.

The third time through, .* goes to matches 0 characters starting at position five (""), but there's a check that makes sure it doesn't match the same starting position and length twice in a row. So it advances the position and tries to match at position six. Since that's outside the string it fails.

Tags:

Regex

Perl