By · Updated

Regex Generator

JavaScript, Python and Go Output From Samples or Preset Patterns

No ratings yet

OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.

How the Regex Generator works

This regex generator builds a pattern without you having to remember the syntax. Choose what the pattern should match and the regular expression appears in the output, ready to copy, with the same pattern quoted for JavaScript, Python, PHP and Go. Paste some lines in the input and each one is tested against it. Or pick "Generalise from my samples", paste a few strings that share a shape — invoice numbers, product codes, log prefixes — and a pattern is built from them.

Regex generator: from a description to a working pattern

A regular expression is a tiny program for matching text, and it is one of the few tools that turns up in almost every language, editor, database and log system you will ever touch. The trouble is the syntax. A correct IPv4 pattern runs to more than 80 characters of brackets, pipes and backslashes, and a single missing escape turns "a literal dot" into "any character". This generator starts from what you want to match, picks a tested pattern, and quotes it correctly for the language you are pasting into, so the only thing left to do is check it against your own data.

Why it matters. Most regex bugs are not dramatic. A validation rule that rejects o'brien@example.ie loses a customer quietly. A log search that matches 10.0.0.1 inside 110.0.0.15 inflates a count that someone then puts in a report. A pattern with nested quantifiers can take seconds on a hostile input, which is how Cloudflare's global outage of July 2019 started: one expression in a firewall rule backtracked catastrophically and pinned CPUs worldwide. Starting from a known shape, and testing it on real lines, removes most of that risk.

Where generated patterns are used

How to read the result

The output box holds the bare pattern. Below it:

Paste test lines into the input, one per line, and the table shows each line with Match or No match and the text that matched. With Anywhere in text, several hits on one line are listed, separated by commas.

Whole line or anywhere in text

The scope choice is the single most important setting. Whole line wraps the pattern as ^(?:...)$, so the entire input must fit; use it for validating a form field. Anywhere in text leaves it bare and adds the global flag; use it for finding every email address in a pasted document. The non-capturing group matters: without it, ^a|b$ means "starts with a, or ends with b", which is a classic source of validation holes.

Generalise from my samples

Choose this option and paste examples that share one shape. The generator splits each line into runs of digits, runs of letters, whitespace and single punctuation marks, checks that every line has the same sequence, and then widens each run to cover all the samples. Given AB-1234 and CD-567, it produces [A-Z]{2}-\d{3,4}: two capitals, a literal hyphen, then three or four digits. Letter classes stay upper-case, lower-case or mixed depending on what the samples contained. If the lines have different shapes, for example one with a space and one without, it says it cannot generalise; split the samples into groups that share a layout and generate one pattern for each.

Tips and pitfalls

To step through a pattern against larger text, with flags and capture groups highlighted, open the regex tester. For URLs and UUIDs that need more than a format check, the URL validator and UUID validator parse the value properly.

FAQ

Why does the generalised pattern say [A-Z]{3} instead of INV?

Because runs of letters are treated as variable text, in the same way runs of digits are. The tool cannot know whether INV is a fixed prefix or one of many three-letter codes, so it chooses the shape. If it is fixed, replace [A-Z]{3} with INV by hand — the rest of the pattern stays correct.

Should I use Whole line or Anywhere in text?

Whole line when you are checking that a value is what it claims to be — a form field, a config value, a cell in a spreadsheet. Anywhere in text when you are finding things inside a larger document — every URL in a page, every date in a log. The unanchored version will happily match bob@example.com inside xbob@example.comx, which is fine for extraction and wrong for validation.

Are these patterns strict enough to validate user input?

They are strict enough to reject typos and malformed input, which is the job of a front-end check. They are not proof that an email is deliverable, a card number is issued, or a domain exists. For anything where a wrong accept is costly, use the pattern as the first gate and confirm the value properly afterwards.

Why do the JavaScript and PHP versions have extra backslashes?

Both languages put the pattern between / delimiters, so any / inside the pattern has to be written \/ or the literal ends early. Python and Go have no delimiter, so the pattern is shown unchanged. The output box holds the bare pattern, which is what you want anywhere delimiters are not part of the syntax.

Can it generate a pattern with capture groups or alternation?

No. Generalised patterns are a single flat sequence, and the presets use only non-capturing groups. If you need to capture the year out of a date or accept two different formats, generate the closest pattern here, then add the parentheses or the | yourself in the regex tester where you can see the groups being captured.

See also: all Dev Utilities tools, Formatters & Validators, Data & Encoding, Security & Identifiers, SEO, Accessibility, AI.

Related tools