Text Editor - Find Text Using Regular Expressions

It’s in our muscle memory. Press Ctrl+F in your favorite text editor to find something, provide find criteria, and hit the Find button. As always, we usually stick with the simple Find functionality of the text editor as it serves almost all of our basic search needs. But beneath Find dialog box, there is a powerful Find option, Use Regular Expressions which most seldom use.

Visual Studio 2019 - Find in Files

Able to recall a correct regular expression when we need it most is rare. Which often leads to the hassle of googling regular expression and trying few failed attempts, and going back to simple Find. This is where following regular expressions cheatsheet comes handy for me.

Regular Expressions:

  • Have foo anywhere and bar later in the same line

    .*foo.*bar

  • Have foo at the beginning of the line

    ^\s{2,}foo

  • Have foo at the beginning of the line and bar later in the same line

    ^\s{2,}foo.*bar

  • Have foo in the commentted line

    .*//.*foo

  • Have foo inside double quotes

    ^.*".*foo.*"

  • Have foo at the beginning or end of the word

    ^.*".*\bfoo.*"

  • Have foo at the end of the line

    foo\r?\n

  • Have foo); at the end of the line

    foo\);\r?\n

  • Have foo but not bar in the same line

    ^.*foo.*(?!bar)

  • Have foo and { in the next line

    .*foo.*\r?\n.*{