Tuesday, January 4, 2011

References

/(\d{1,3}\.){3}(\d{1,3})/
People sometimes expect this to capture into $1, $2, $3, $4, but that's wrong
It has only two pairs of parentheses, so it captures only $1 and $2
Why? Isn't the {3} supposed to `repeat three times'?

What does it do with 130.91.6.1?
Start copying, copy the 130 into $1, stop copying, repeat
Start copying, copy the 91 into $1, stop copying, repeat
Start copying, copy the 6 into $1, stop copying, go on
Start copying, copy the 1 into $2, stop copying, end of string

End result: Only 6 is in $1.
Solution: Use m//g (coming up) or split

1 comment:

Anonymous said...

Good!!!

by
Gopal