tags:

views:

84

answers:

2

Would an anchor like "^" or "\A" at the beginning of this regex make any sense - any difference?

$string =~/(.*)([a-z])$/
A: 

No, because of the greedy nature of regular expression matching that regex will pull everything before the final letter of the string, provided the last character is a letter.

It would make sense, just not any difference.

OmnipotentEntity
+5  A: 

Yes, either ^ or \A will cause the regex to not match if there is a newline anywhere before the letter, because .* (zero or more of any characters except newline) will no longer match up to the letter before the end.

Without the beginning anchor, the regex will match from after the last newline through the end of the string (or through the letter before the newline at the end, if there is a newline).

ysth
No, it won't make it fail if there is a newline *anywhere* in string, provided that that anywhere is at the end.
tchrist
@tchrist: thanks
ysth