Regular Expressions Regex modifiers (flags) MULTILINE modifier


Another example is a MULTILINE modifier (usually expressed with m flag (not in Oniguruma (e.g. Ruby) that uses m to denote a DOTALL modifier)) that makes ^ and $ anchors match the start/end of a line, not the start/end of the whole string.

/^My Line \d+$/gm

will find all lines that start with My Line, then contain a space and 1+ digits up to the line end.

An inline version: (?m) (e.g. (?m)^My Line \d+$)

NOTE: In Oniguruma (e.g. in Ruby), and also in almost any text editors supporting regexps, the ^ and $ anchors denote line start/end positions by default. You need to use \A to define the whole document/string start and \z to denote the document/string end. The difference between the \Z and \z is that the former can match before the final newline (LF) symbol at the end of the string (e.g. /\Astring\Z/ will find a match in "string\n") (except Python, where \Z behavior is equal to \z and \z anchor is not supported).