Alex Rivera | Logout

What is the difference between ^ and \A , $ and \Z in regex?

Asked 2010-11-22T20:42:00.050
27

In regular expressions:

  • What is the difference between ^ and \A?
  • What is the difference between $ and \Z?
Edit
Report

2 Answers

19

In single-line mode, $ matches either the end of the string, or just before the newline at the end of the string. In multiline mode $ matches before each newline in the string. \Z always matches only the end of the string regardless of the line mode. Same with ^ versus \A.

answered 2010-11-22T20:46:06.530
9

See perldoc perlre.

The \A and \Z are just like "^" and "$", except that they won't match multiple times when the /m modifier is used, while "^" and "$" will match at every internal line boundary. To match the actual end of the string and not ignore an optional trailing newline, use \z .

answered 2010-11-22T20:47:58.863

Your Answer