Regular Expressions Anchor Characters: Caret (^) Multiline modifier


By default, the caret ^ metacharacter matches the position before the first character in the string.

Given the string "charsequence" applied against the following patterns: /^char/ & /^sequence/, the engine will try to match as follows:

  • /^char/

    • ^ - charsequence
    • c - charsequence
    • h - charsequence
    • a - charsequence
    • r - charsequence

    Match Found

  • /^sequence/

    • ^ - charsequence
    • s - charsequence

    Match not Found

The same behaviour will be applied even if the string contains line terminators, such as \r?\n. Only the position at the start of the string will be matched.

For example:



However, if you need to match after every line terminator, you will have to set the multiline mode (//m, (?m)) within your pattern. By doing so, the caret ^ will match "the beginning of each line", which corresponds to the position at the beginning of the string and the positions immediately after1 the line terminators.

1 In some flavors (Java, PCRE, ...), ^ will not match after the line terminator, if the line terminator is the last in the string.

For example:



Some of the regular expression engines that support Multiline modifier:

  • Java

    Pattern pattern = Pattern.compile("(?m)^abc");
    Pattern pattern = Pattern.compile("^abc", Pattern.MULTILINE);
  • .NET

    var abcRegex = new Regex("(?m)^abc");
    var abdRegex = new Regex("^abc", RegexOptions.Multiline)
  • PCRE

  • Python 2 & 3 (built-in re module)

    abc_regex = re.compile("(?m)^abc");
    abc_regex = re.compile("^abc", re.MULTILINE);