On 3/7/2011 4:47 AM, Stan Hoeppner wrote:
I was taught to always start my expressions with "/^" and end them with
"$/". Why did Steven teach me to do this if it's not necessary?
That's good advice when you're actually matching something.
The special case of .* means, as you know, "anything or
nothing". There's never a case where it's necessary to
explicitly match a leading or trailing "anything or nothing".
Consider:
/^.*foo$/
match the string beginning with anything or nothing, ending
with foo.
can always be simplified to:
/foo$/
match the string ending with foo.
This works the same without the ending $ anchor (contains foo,
rather than ends with foo), but helps the illustration.
(In the other special case where you're using $1, $2, etc.
substitution in the result, you might need some form of
/^(.*foo)$/ to fill the substitution buffer, but that's about
substitution, not about matching.)
-- Noel Jones