Skip to content

Instantly share code, notes, and snippets.

@deebuls
Last active February 1, 2018 12:42
Show Gist options
  • Select an option

  • Save deebuls/8ceba6235a93141415e85cbf349f5735 to your computer and use it in GitHub Desktop.

Select an option

Save deebuls/8ceba6235a93141415e85cbf349f5735 to your computer and use it in GitHub Desktop.
Regex learnings

Regex used by me

  1. For searching lines with only a single word .
  • ^\h*\w+\h*$
  • ^ - Start of line
  • \h* - optional leading horizontal whitespac
  • \w+ - 1 or more word symbols You can use \S instead of \w to match any non-whitespace symbols.

To insert a linebreak, just replace with \r\n$0. You do not need any capturing groups since you can always reference the whole match with $0 (or $&).

  1. Find empty lines
  • ^\s*$

  • Explanation:

    • ^ is the beginning of string anchor.
    • $ is the end of string anchor.
    • \s is the whitespace character class.
      • is zero-or-more repetition of. In multiline mode, ^ and $ also match the beginning and end of the line.

    References:

    regular-expressions.info/Anchors, Character Classes, and Repetition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment