Tuesday, January 4, 2011

Strings that contain newlines

/s: Make . match newline (normally it doesn't)
/m: Make ^ match at beginning of line (after a newline) rather than beginning of string, and similarly $]

Example: Suppose $message contains an entire mail message.
        ($subject) = ($message =~ /^(Subject:\s+(.|\n\s)*)$/m);Extracts Subject field.

If you use /m, use \A and \Z to get the old meanings of ^ and $: Match at beginning or end of string only
Recall that $ normally matches before a newline at the end of the string. \Z does that too.
If you really want to match only at the end of the string, use \z

Perl 6 will fix this mess; /s and /m are going away:

  ^      beginning of string
  $      end of
^^      beginning of line
$$      end of line
.         match any single character
\n       match a newline
\N      match any single character except a newline

No comments: