6/2/2014 - 4:03 AM

Useful Regular Expressions

Useful Regular Expressions

When I'm working with my growing collection of text files, I find some of these regular expressions to be useful.

See also:

Strip a Tag, and its contents, include linebreaks


find any string: .*

example find/replace. to remove quotes: find: "(.)" replace: $1 This works by using a capturing group (.), which is referred to with $1 in the replacement text.

find with "does not contain"

^((?!find lines without this string).)*$

or, find all the text except for "estimated reading time: X min" where X is an integer. Here, the (.|\n) will include linebreaks, where above it's only . to find lines.

^((?!estimated reading time: \b[0-9]+\b min)(.|\n))*$

find any number (zero or greater)


find a number, excluding a specific number


In this example, we're searching for any number that is NOT 2020. note the number of digits in the second part.

find anything between quotes:


for a similar approach, this will find one or more characters between quotes, but it will not return empty quotes:


newline: \n

Look for a tag

Let's say you have some YAML tags like tags: [landscape,portrait] if you have lots of different tags, but only want to find a specific one, a search can help. For example, a regex search like: tags: .*landscape.* will even return other things like tags: [tag1,othertag,landscape] or it will return just plain tags: [landscape]

Fix linebreaks

Often I'm copying text out of a PDF or similar and the line breaks aren't the way I want them. Instead of many short lines for each paragraph, I want one paragraph to be a single line of text, with a blank line between paragraphs.

I can fix this with regex in two steps:

  1. find all the double linebreaks [\r\n][\r\n] and replace them with a placeholder string like -------placeholder--------. don't worry, that placeholder will go back to being the space between paragraphs.
  2. now that we know where the paragraph breaks belong, it is safe to get rid of all the line breaks. replace [\r\n] with nothing.
  3. you should now have one single line of text for the entire document, with the placeholder string in place of the paragraph breaks.
  4. replace -------placeholder-------- with the double linebreaks [\r\n][\r\n]
  5. done!

Find any URL


Find HTML Tags and Content


it works regardless of attributes. This will find an HTML element, its opening and closing tag, and any included content, regardless of attributes. So it will find the “div” elements that have any attributes, and also those that don’t have attributes. You can change the “div” at the beginning and end to the HTML element that you are searching for.

Find anything between two strings


find anything between the comment and the h1 tag. this will even include linebreaks.

Useful regex during manuscript cleanup

My usual process for moving a book manuscript from Word to kramdown involves:

  1. Convert Word to markdown using Pandoc. To make this easy, I use this batch script in Windows.
  2. Run a series of regex search-and-replaces. These vary from job to job, to suit the book. This document lists common ones. I use Sublime Text for this, but these should work in most good text editors (e.g. Atom, Brackets).
  3. Manually fix and improve the markdown referring visually to the source Word document. This takes a human, because many authors use formatting for semantic purposes and that formatting doesn't convert to markdown.

In each example below, the first line is find, the second line replace (unless replace should be blank to delete content).

Never use these to replace-all in a long manuscript in one automated step. They are for quickly moving through a manuscript, where you visually confirm every replace.

Copy-paste to split book into separate chapter files

I usually do this at the end, once I've cleaned up the markdown for a whole book in one file. First I make sure I've put YAML frontmatter markers – two lines of --- – at the start of every file-to-be. It doesn't matter if they contain YAML or not.



  • (?s) says this regex will match newline characters when we say 'match anything'.
  • ^(---)$ matches --- if it starts and ends a line, i.e. it's the only thing on the line.
  • .+? matches one or more of anything else, non-greedily, i.e. until it finds what matches next.
  • ^(---)$ again, matches --- on its own on a line.
  • .+? again, matches one or more of anything else.
  • (?=^---$) says stop when you see three hyphens on their own line again (i.e. the next doc's frontmatter).

In short, it selects the content from one YAML frontmatter block (two lines of three hyphens, which may or may not have YAML between them) until the next YAML block begins.

So if you have a file of markdown and you want to split it into separate files, first add the two ---s at the start of each piece of content you want in a new file, then use this regex to select it and the content, up till the next YAML block. Note it will not find the last YAML block and its content.

In Sublime Text in Windows, F3 will select the next found text. So to find and create a new file, I press: F3 > Ctrl X > Ctrl N > Ctrl V > Ctrl S [save to filename] > Ctrl W. You can get pretty quick at that, especially if you hold down Ctrl.

This will find, cut, open a new file, paste the contents, save them (prompting you for a filename), and close the new file.

Then repeat.

Add non-breaking space in range of numbers

(\d) (\d)

Fix inline spans broken by a space after a word before the closing *

This regex finds a very common problem with conversion from MS Word (and other similar programs), where italics have been applied not just to a word but to the space after the word. This happens in Word when a user double-clicks a word to highlight it, before making it italic. When double-clicking a word in Word, Word also highlights the space after the word. When this converts to markdown, the markdown syntax breaks, because *this *is broken while *this* is correct.

Single-word search

Note: this regex only finds single-word instances of this problem, not phrases. E.g. it will fix We watched *Oliver *today, but not We watched *Oliver Twist *today.


This regex (1) finds one or more asterisks followed by (2) any word character or punctuation (except asterisks), followed by (3) one or more spaces, followed by (4) one or more asterisks. The replace simply switches the space and the final asterisks.

Phrase search

This regex is more powerful and finds the same problem but in words or phrases. We haven't tested it a lot, so don't use it for global replaces: eyeball every change it makes.

((?<=\s)|(?<=^))(\*+[\w !"\#$%&'()+,\-./:;<=>?@\[\\\]^_`{|}~]+)(\s+)(\*+)

This regex works the same way as single-word search, except that it allows spaces in the matching phrase, and looks for the presence of either a beginning of line or another space before the first asterisk.

Remove image width and height inherited from images in docx


Wrap all images in an Electric Book figure blockquote

\n> \1\n{:.figure}\n

Simplify indentation in lists by reducing space after list marker to one space


Note the space at the end of the replace expression.

Remove non-kramdown markdown ^ around superscripts after numbers


Note: afterwards, do a manual search for ^, because if in the docx source a following character was mistakenly made superscript, too (e.g. 3^rd)^), this regex won't find it.

Wrap the opening words of a paragraph in a span

This particular regex finds the first five words of a paragraph and wraps them in a strong span with a kramdown class attribute of myclass (i.e. <strong class="myclass">).

It looks for a line break, then a word, then three words preceded by a space. Then the replace wraps that all in double asterisks with a kramdown inline attribute.

To change the number of words it selects, change the 3 in braces, near the end of the regex. It should be one less than the number of words you want to select.

The replace regex is simple enough to edit. E.g. change to one asterisk for an em span, and of course change yourclass to the class you need.

Remember to turn on the 'Preserve case' option in your editor before using this


Find an email address

See this post for details:


Find URLs

URLs are really hard to find. This gist from John Gruber is your best best.

You can use it to quickly move through a text, replacing where necessary with markdown links, e.g. usng a replace like \[\1\](http://\1).

Replace single line breaks, keeping empty lines

This post explains:

$1 $2

Replace ##Close-up double-hash headings### with ## kramdown headings

This finds, at the start of a line, one or more hashes, then a string (hopefully heading text), then another string of hashe before a line ending. It replaces it with the same number of hashes at the start, a space before the heading text, and no trailing hashes.

\n\1 \2\n

Find double quotes inside double quotes in Liquid tag parameters

E.g. you may need to debug Liquid tags like {% include figure markdown="This is a "figure"" %}


Replace named HTML entities with numeric entities

EPUB3 does not allow named HTML entities (e.g. &nbsp;), only numeric ones (&#160;). That's a pity because named entities are easier for humans to remember. Where kramdown converts markdown to HTML, kramdown by default replaces both entities with actual unicode characters. But kramdown doesn't reach into block-level elements in HTML islands (actual HTML code inside your markdown file), unless you add the attribute markdown="1" to the element's tag.

In tables, this is a PITA, because you'd have to add markdown="1" to every <td> that contained a named entity (you can't apply the attribute to the parent <table>) and hope that processing its content as markdown won't have unexpected side effects.

The sensible solution is to just replace named entities in tables with numeric entities. This most often happens with &nbsp; and &shy;. Here is the regex for that. This will find every table with an &nbsp in it and replace it with the numeric equivalent.

Note that this will replace only the first &nbsp; in each table. If a table has more than one &nbsp in it, you will have to run this find-and-replace again for each one.


You can use the same strings for, say &shy; if you:

  1. replace &nbsp; with &shy; in the find string
  2. replace &#160; with &#173; in the replace string.

Fun demo: fix split infinitives

This will change 'to boldly go' and 'to plainly say' to 'to go boldly' and 'to say plainly', but it will not change 'to plant flowers'.

\1 \3 \2

Add YAML frontmatter before main headings

title: "$1"

# $1