Embed this tool on your site
About this tool
Regex Generator from Text Samples turns one or more sample strings into an escaped regular-expression pattern. Simple mode joins the samples as alternatives. Smart mode extracts a common prefix and suffix and groups the differing middle text. A single sample therefore produces an exact escaped match, while multiple samples produce a pattern that matches those supplied alternatives or their shared prefix/suffix structure. The tool explains the generated pattern visually through the result and includes a test field so you can try additional strings. Treat the output as a starting point: it does not infer semantic formats such as a valid email, date, or IP address from examples, and you should test edge cases in the Regex Tester before using it in code.
Why use it
Escapes special characters like . ( ) and ? for you.
Smart mode factors shared prefixes and suffixes out of a word list.
Built-in test field shows matches as you type.
Saves time when you need a pattern that matches a known list of strings.
Gives a starting point to refine in the Regex Tester.
Runs entirely in your browser.
How to use
- Enter one or more sample strings you want the regex to match (one per line).
- Choose Smart (extract prefix/suffix) or Simple (join with |).
- Click Generate Regex; the pattern appears as /pattern/g.
- Type in the Test input field to see which parts of a string match.
- Click Copy pattern to copy the /pattern/g literal.
- For a single sample, use Simple mode — Smart mode is meant for two or more different samples.
When it helps
- Matching a fixed list of keywords, product codes or file names.
- Turning a list of words into one compact alternation pattern.
- Quickly creating a starting pattern to refine in a regex tester.
- Escaping a literal string that contains regex metacharacters.
- Seeing how a common prefix or suffix can be pulled out of alternatives.
Examples
Expected result/hello world/g
Expected result/555-(?:1234|5678)/g
Expected result/(?:cat|dog)/g
Tips
- Use Simple mode for a single sample; Smart mode needs at least two different samples.
- After generation, paste the regex into the Regex Tester tool to verify it against additional samples.
- The pattern only matches the strings you listed — add more samples to cover more cases.
- Use anchors (^ and $) when validating whole strings so partial matches are rejected.
- Remove the surrounding slashes and g flag when pasting into Python, PHP or Java code.
Frequently Asked Questions
Does it generate regex for all programming languages?⌄
The output only uses escaped literals, non-capturing groups (?:...) and |, which work the same in JavaScript, Python, PHP, Java and most other engines. The copied text includes JavaScript-style slashes and a g flag (/pattern/g); drop those for other languages.
Will it generalize from my examples?⌄
No. The pattern matches only the exact strings you enter (or their shared prefix/suffix plus one of the listed middles). It does not detect formats such as emails, phone numbers, dates or IPs and never adds character classes like \d or quantifiers like +.
What is the difference between Smart and Simple mode?⌄
Simple escapes each sample and joins them with |. Smart finds the text all samples start and end with and puts only the differing middles in a (?:a|b) group, so apple and apricot become ap(?:ple|ricot).
Why does Smart mode repeat my text when I enter one sample?⌄
With a single sample (or identical samples) the shared prefix and suffix are the whole string, and Smart mode joins them, so the text appears twice. Use Simple mode for one sample.
Can I test the generated regex?⌄
Yes. Type in the Test input field under the result to see the match count and matched text. For more checks, paste the pattern into the Regex Tester tool on this site.
Does it add anchors like ^ and $?⌄
No. The pattern has no anchors, so it also matches the samples inside longer text. Add ^ and $ yourself for whole-string validation.
Glossary
- Regular expression (regex)
- A string-based pattern language for matching and extracting text content.
- Character class
- A set of characters in brackets like [a-z] or [0-9] that match any one character from the set.
- Quantifier
- A regex symbol like *, +, or ? that specifies how many times the preceding element can occur.
- Anchor
- A regex element like ^ (start) or $ (end) that asserts position rather than matching characters.
- Capture group
- Parentheses in a regex that group elements and capture the matched text for later use.
- PCRE
- Perl Compatible Regular Expressions — a regex syntax variant supported by most modern engines.
A LITTLE HELP. A LOT DONE.
Good tools.
Great flow.
Your shortcut to the everyday. Create, convert, calculate,
and get back to what you love.
✳
✳
✓ Free to use✓ No sign-up✓ 500+ possibilities