Skip to content

Instantly share code, notes, and snippets.

@ArtskydJ
Last active April 19, 2016 00:56
Show Gist options
  • Save ArtskydJ/3ed7d3b089175e6f41f1 to your computer and use it in GitHub Desktop.
Save ArtskydJ/3ed7d3b089175e6f41f1 to your computer and use it in GitHub Desktop.
Common/hard-to-write regexes

regexes

Common/hard-to-write regular expressions

Paragraph without an ending period

Find lines that are at the end of paragraphs that don't have periods at the end.

  • ^[^\n][^(\.\n)]+[^\.]\n\r?\n\r?

Example:

This is a paragraph and it
has newline characters inside
it.

You want to detect lines that
don't have trailing periods,
but you don't want to detect
the lines without periods
that are in the middle of a
paragraph

Well, this is the regex for
you!

UUID v4

Find v4 UUIDs.

  1. [a-f0-9-]{36}
  2. ([a-f0-9]{4}-?){8}
  3. [a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}
  4. [a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}
  5. [a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}

Example

ee-eeeeee-eeeeeeeeee-eeeeeeeeeeeeee-    #1
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee    #1
------------------------------------    #1
eeee-eeee-eeee-eeee-eeee-eeee-eeee-eeee- #2
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee         #2
eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee      #3
eeeeeeee-eeee-4eee-eeee-eeeeeeeeeeee       #4
eeeeeeee-eeee-4eee-9eee-eeeeeeeeeeee        #5

Newlines

Find newlines. Does not catch \n\n or \r\r as one new line.

  • \r - Mac
  • \n - Linux
  • \r\n - Windows
  • (\r\n|\n|\r) - Windows, Mac, and Linux

Stray tabs

Catch tabs that are not being used as indentation.

  • [^\t\n\r]\t

Missing semicolons in ColdFusion

Catch missing semicolons in languages that require them. Assumes newline = \r\n, and one or fewer statements per line.

Allows lines without semicolons to end with ,, {, }, &, and >. Catches other lines.

  • ^[^;\r\n]+[^\[;,\{\}&>]\r\n

Not very good around full/multi-line comments, but that's what humans are for.

Markdown headers

Find titles in markdown strings.

  • (?:^\#(.+)|(.+)(?:\r\n|\r|\n)(---+|===+)$)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment