sid_com
sid_com

Reputation: 25137

Regex anchor question

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

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

Upvotes: 1

Views: 244

Answers (2)

ysth
ysth

Reputation: 98433

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).

Upvotes: 5

OmnipotentEntity
OmnipotentEntity

Reputation: 17131

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.

Upvotes: 1

Related Questions