Regex Generator from Sample
Writing a regex by hand for something like an order number, an invoice reference or a date is fiddly and easy to get wrong. Paste one example string that matches the format you want, and this tool reads it left to right, turning each run of digits, uppercase letters and lowercase letters into a character class of the right length while keeping punctuation and symbols as literal characters. You get an anchored pattern to use as a starting point and refine by hand.
How it works
-
1
Enter a sample
Type or paste one example string that represents the format you want to match, such as `Order #12345 placed on 2026-04-16`.
-
2
Generate the pattern
The tool scans the sample character by character and replaces each run of the same kind of character with a character class sized to its length.
-
3
Read the breakdown
Below the pattern, a component list explains each piece, so you can see which part matched digits, letters or literal text.
-
4
Copy and refine
Use the pattern as a starting point. Adjust the fixed lengths or the classes by hand so it fits the full range of your real data.
How the pattern is built
The tool reads the sample from left to right. Whenever it meets a run of the same kind of character, it emits a character class sized to that run:
- a run of digits becomes
\d{n}, wherenis how many digits there are - a run of uppercase
A-Zbecomes[A-Z]{n} - a run of lowercase
a-zbecomes[a-z]{n} - a single space becomes
\s, and several spaces become\s{n} - anything else (punctuation, symbols, accented or non-Latin letters) is kept as an escaped literal character
The finished pattern is wrapped in ^ and $ so it matches the whole string from start to end, not just a fragment.
Example
For the sample Order #12345 placed on 2026-04-16 the tool produces:
/^[A-Z]{1}[a-z]{4}\s\#\d{5}\s[a-z]{6}\s[a-z]{2}\s\d{4}\-\d{2}\-\d{2}$/
Order is one uppercase letter followed by four lowercase letters; 12345 becomes \d{5}; and the date 2026-04-16 becomes \d{4}\-\d{2}\-\d{2}.
What it does and does not do
- The lengths are fixed.
12345becomes\d{5}, which matches exactly five digits, not “one or more”. If the count varies in your data, change{5}to+or a range such as{3,8}by hand. - It works from a single sample. It mirrors the structure of the one string you give it, so it cannot generalise across several examples on its own. Feed it the most representative string.
- Only
A-Zanda-zcount as letters. Accented and non-Latin characters are kept as literals, so replace them with a class such as\wor\p{L}if you want them to match a wider set. - A regex cannot express everything. Things like balanced brackets are beyond any regex, no matter the sample. For nested structures you need a parser, not a pattern.
A good workflow
- Generate a pattern from one clear, representative sample.
- Relax the fixed counts (
{5}becomes+or{3,8}) wherever the length varies. - Paste the pattern into the Regex Tester with a larger set of real strings.
- Adjust until it matches everything it should and nothing it should not.
Frequently Asked Questions
One. The generator reads a single example string and mirrors its structure. Pick the string that best represents your format, then widen the pattern by hand for the other cases.
The tool counts the exact characters in your sample, so five digits become \d{5}. If the count varies in real data, replace {5} with +, * or a range such as {3,8}.
The anchors make the pattern match the whole string from start to end. Remove them if you want to find the pattern inside a longer piece of text.
It recognises A-Z and a-z as letters. Any other character, including accented letters, Cyrillic or CJK, is kept as a literal. Swap it for a class like \w or \p{L} by hand if you want it to match more.
Standard syntax (\d, [A-Z], {n}) that works unchanged in PCRE, JavaScript, Python and most other engines.
Related Tools
ASCII Table Reference
Full ASCII table from 0 to 127 with decimal, hex, octal, binary, standard names and HTML numeric-reference notation, including NUL, LF and DEL.
Color Palette Generator
Generate monochromatic, analogous, complementary, triadic or tetradic color palettes from a base HEX color and export copy-ready CSS variables.
FPS Counter
Measure browser FPS with requestAnimationFrame, smoothing, min/max frame rate, warnings and an optional graph. Runs locally with no upload or API.
HTML Character Reference
Searchable list of HTML entities, their named and numeric codes, and a one-click copy for special characters and symbols.
HEX Color Picker
Pick or enter a HEX colour and get RGB, HSL, approximate CMYK, relative luminance and contrast ratios against white and black.
Keyboard Shortcut Reference
Search documented default keyboard shortcuts for VS Code, Chrome and Bash with GNU Readline on macOS, Windows and Linux.
Tool available in other languages
- مولّد Regex من عيّنة [AR]
- Générateur de Regex à partir d'un exemple [FR]
- 샘플로 정규식 생성 [KO]
- Generator Regex dari Contoh [ID]
- Regex-Generator aus Beispiel [DE]
- Generator Regex z przykładu [PL]
- Regex-generator uit voorbeeld [NL]
- Regex-generator från exempel [SV]
- ตัวสร้าง Regex จากตัวอย่าง [TH]
- サンプルから正規表現を生成 [JA]
- Trình tạo Regex từ mẫu [VI]
- Generador de Regex a partir de una muestra [ES]
- Gerador de Regex a partir de um exemplo [PT]
- Örnekten Regex Oluşturucu [TR]
- 从示例生成正则 [ZH]
- Generatore di Regex da un esempio [IT]
- Генератор Regex из примера [RU]