A regular-expression anchor is a zero-width assertion: it consumes no characters, but requires the match to occur at a structural position in the string. Anchors complement character matching by constraining where matching may proceed. section 11.5.3 section 11.5
Filtering comment lines
The regexp /^\s*#/ matches a line beginning with optional whitespace followed by #. It matches " # Pure comment!" but not " x = 1 # Code plus comment!", because the latter comment does not begin the line. section 11.5.3
Common Ruby anchors express these positions and boundary conditions:
- \Z asserts the end of the string while allowing an optional trailing newline. section 11.5.3
Anchors differ from lookahead and lookbehind assertions in purpose: anchors constrain structural positions, while assertions generally require surrounding matching conditions without consuming characters. Together with quantifiers and modifiers, they refine regexp behavior. section 11.5 section 11.5.3


