← Resources RESOURCE
A collection of practical examples for reformatting text with capture groups and replacement strings.

The key to replacement is splitting the matched part into capture groups and rearranging them. Reference numbered groups with $1·$2 and named groups with $<name>. For example, capturing a date with (\d{4})-(\d{2})-(\d{2}) and replacing with $3/$2/$1 turns 2026-07-02 into 02/07/2026.

Cleaning up whitespace is a frequently used recipe. Replace runs of whitespace \s+ with a single space, and remove leading and trailing whitespace on lines by replacing ^\s+|\s+$ with an empty string. For multi-line text, you must also turn on the m flag so it applies to the start and end of each line.

Reformatting by keeping only digits is also common. Capturing the digits in a phone number with (\d{3})(\d{4})(\d{4}) and replacing with $1-$2-$3 produces a consistent format. In this tool's Replace tab you can refine the result while watching it in real time.

It helps to know the special replacement symbols too. $& is the whole match, $` and $' mean the context before and after the match, and a literal dollar sign is written as $$. This tool supports string replacement; for complex conditional replacement, it's better to use a function replacement in a programming language.