Ruby regex: ^ matches start of line even without m modifier?

  • start of the line: ^
  • end of the line: $
  • start of the string: \A
  • end of the string: \z

Use \A instead of ^.

Ruby regex reference: http://www.zenspider.com/ruby/quickref.html#regexen


Your confusion is justified. In most regex flavors, ^ is equivalent to \A and $ is equivalent to \Z by default, and you have to set the "multiline" flag to make them take on their other meanings (i.e. line boundaries). In Ruby, ^ and $ always match at line boundaries.

To add to the confusion, Ruby has something it calls "multiline" mode, but it's really what everybody else calls "single-line" or "DOTALL" mode: it changes the meaning of the . metacharacter, allowing it to match line-separator characters (e.g. \r, \n) as well as all other characters.

Tags:

Ruby

Regex