Anchor start

2 snippets in Regular Expressions

Also written as start anchor

RXRegular Expressions

^ Start of String

RX · Basic Patterns
Syntax
^pattern
Example
JS:  /^Error/.test('Error: file missing')   // true
JS:  /^Error/.test('An Error occurred')   // false
Py:  bool(re.match(r'^Error', 'Error: file missing'))  # True
Output
true, false, True

Note Matches position at start of string. With the m (multiline) flag, ^ matches the start of each line instead. Do not confuse this with [^...] inside a character class, which means negation.

^ Start Anchor

RX · Anchors & Boundaries
Syntax
^pattern
Example
JS:  /^#!/.test('#!/bin/bash')    // true
JS:  /^#!/.test('echo #!/bin')   // false
Py:  bool(re.match(r'^#!', '#!/usr/bin/env python'))  # True
Output
true, false, True

Note Without the m flag, ^ matches only the very beginning of the entire string. With the m flag, it matches the start of each line (after every newline character).

Frequently asked questions

How does Regular Expressions handle anchor start?
Regular Expressions covers this with 2 copy-ready snippets on this page. The "^ Start of String" snippet in Regular Expressions uses `^pattern`.
Which pattern does the Regular Expressions example use?
The "^ Start of String" snippet uses `^pattern`, from the Basic Patterns section of the Regular Expressions cheat sheet.
What other Regular Expressions snippets are shown for "anchor start"?
Besides "^ Start of String", this page also shows "^ Start Anchor".
Is there anything to watch out for?
Yes. For "^ Start of String": Matches position at start of string. With the m (multiline) flag, ^ matches the start of each line instead. Do not confuse this with [^...] inside a character class, which means negation.