Regex – Cheat Sheet – Basic – Anchors

Regex – Cheat Sheet – Basic – Anchors

All the examples below have been tested in Notepad++. Notepad++ can be found here. These examples can also work in other platforms with some minor syntax changes to them.

Anchors

Types Of Anchors

  • ^ used to denotate the start of a string or line
  • $ used to denote the end of a string or line

Use Case for ^

Let’s say we have the list below and we want to add the letter G to the start of all the strings below.

aa
ab
ac
ad
ae
af

Press Ctrl + H to bring up the replace window in Notepad++ or your set shortcut key for it. In order for this to work you have to set the following:

  • Ensure that the text cursor is at the start of the list or that all the text is selected (by using Ctrl + A).
  • ^ in Find what section.
  • G in Replace with section.
  • Ensure that the Regular expression radio button is selected.
  • Click on the Replace All button.

Before

Before Regex Applied

After

After Regex Applied

Use Case for $

Let’s say we have the same list but instead of adding G to the start we want to add it to the end.

aa
ab
ac
ad
ae
af

Press Ctrl + H to bring up the replace window in Notepad++ or your set shortcut key for it. In order for this to work you have to set the following:

  • Ensure that the text cursor is at the start of the list or that all the text is selected (by using Ctrl + A).
  • $ in Find what section.
  • G in Replace with section.
  • Ensure that the Regular expression radio button is selected.
  • Click on the Replace All button.

Before

Before Regex Applied

After

After Regex Applied

Leave a Reply