Regex: Find Every Line that Begins with a Specific Word

To find every line that begins with a specific word, use the following regex command:

^(Word*)

Example

Let’s say you have a bunch of markdown articles that have a metadata structure, such as:

---
publishDate: 2021-01-18
title: "Bitcoin vs. Ethereum"
template: post
thumbnail: "../../thumbnails/crypto.png"
slug: bitcoin-vs-ethereum
tags:
  - Crypto
  - Ethereum
  - BitCoin

But unfortunately, someone misspelled Bitcoin when they created the meta template. Bitcoin is not spelled with capital C as in the tag example above.

To find every line that begins with - BitCoin, run this regex command:

^(  - BitCoin.*)

Notice that all the spaces in the regex command match the spaces in the metadata example.

How the regex command works:

  • ^ means that any match must be found at the start of a line.
  • ( - BitCoin.*) matches the text BitCoin on that line, including hyphen and surrounding white space.

Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊

Kofi

Share & Discuss on