Regex Generator from Examples
List a few good and bad examples, pick the pattern that accepts all of the first and none of the second, read what every piece means and copy ready code.
Berjalan dalam pelayar anda. Fail anda tidak pernah meninggalkan peranti. Lihat cara menyemaknya
…
Cara melakukannya
- 1Add positives
Type strings that should match, one per line, or pick a preset.
- 2Add negatives
Type near misses that must be rejected.
- 3Set options
Choose whole-string matching and ignore case.
- 4Pick a candidate
Press Use on the pattern with all matches and 0 wrong.
- 5Check it
Read the part-by-part explanation and test it on free text.
- 6Export
Choose a language tab and press Copy code.
Tentang alat ini
Most people who need a regular expression know exactly what the data looks like but not how to spell the pattern: order numbers like AB-1234, dates like 2024-01-05, version tags like v10.2.3. Writing the pattern from scratch means remembering classes, quantifiers and escaping rules, and copying one from a forum often brings a pattern that is far looser or stricter than intended. This generator works from examples instead. Put the strings that must match in one box and the strings that must be rejected in the other. The tool breaks every positive example into runs of digits, lowercase letters, uppercase letters, spaces and literal punctuation, compares the runs across examples and builds several candidates, from strict ones that keep exact classes and lengths to looser ones that merge letters, letters and digits, or allow one or more of each. It also tries a common prefix and suffix, and a plain list of the examples as a last resort. Every candidate is tested against both boxes, and the list is ranked so that patterns which accept all positives and reject all negatives come first.
Choosing between candidates
Each candidate shows how many positives it matched and how many negatives it wrongly matched. When several are correct, the stricter one usually wins, because it rejects strange input you have not thought of yet. If the examples vary in length, a pattern with one or more quantifiers is preferred so new values of a different length are still accepted. Add a counter-example whenever a candidate is too loose; the ranking updates while you type.
Explanation and testing
The selected pattern is split into parts with a plain-English line each: an uppercase letter exactly 2 times, the text -, a digit exactly 4 times. You can edit the pattern by hand and the explanation, the green and red marks next to every example and the highlighted matches in the free test text all follow your change. In the test text, an anchored pattern is wrapped in word boundaries so it can find codes inside a sentence.
Export
Ready-to-paste code is written for JavaScript, Python, PHP, Java, C# and Go, with the escaping each language needs: a regex literal with escaped slashes, a raw string with re.fullmatch or re.search, a preg_match call with delimiters, a doubled-backslash Java string with matches or find, a verbatim C# string and a Go raw string. The ignore-case option is translated into each language's flag.
Limits
The generator learns structure, not meaning: it cannot know that a month must be 01 to 12 or that an email domain needs a dot unless your negatives show it. Very different examples produce an alternation of several shapes, which may be better split into separate checks. It is not a validator for email addresses, IBANs or postcodes, which have official rules; a pattern from a handful of samples should not replace those rules.
Contoh berselesaian
A support team must recognise ticket codes. They enter three positives, AB-1234, XY-0001 and QQ-9999, and four negatives, ab-1234, AB1234, AB-123 and ABC-1234. The top candidate is [A-Z]{2}-\d{4}, which matches 3 of 3 positives and 0 of 4 negatives. The looser [A-Z]+-\d+ is ranked lower because it wrongly accepts AB-123 and ABC-1234. In the test sentence the pattern highlights AB-1234 and CD-5678 but not ab-0000, and the Python export reads re.compile(r"^[A-Z]{2}-\d{4}$") with pattern.fullmatch.
Ringkasnya
- Kategori: Pembangun & data
- Berjalan dalam pelayar, tanpa pendaftaran, tanpa muat naik.
- Maklumat yang anda masukkan kekal pada peranti anda.
- Tersedia dalam bahasa Inggeris.
- Kandungan disemak kali terakhir:
- Sumber rasmi: ECMAScript RegExp (ECMA-262) and Python re module documentation
Soalan lazim
How many examples do I need?
Two or three positives are enough for regular codes. Add counter-examples that are almost right, such as wrong case or one digit short, because they push the result towards the pattern you really want.
What does Whole string must match do?
It wraps the pattern in ^ and $ so a value must consist of the pattern and nothing else, which is right for form validation. Switch it off to find the pattern inside longer text.
Why did I get a list of my examples?
When the examples have nothing in common that separates them from the negatives, the only safe pattern is the list itself. Add more positives with the same shape so a general pattern can be found.
Can I edit the generated pattern?
Yes. Type in the Pattern field; the explanation, the example marks, the test text and the exported code all update at once, and Undo brings back the earlier version.
Does it work for non-English letters?
The class detection uses A to Z and a to z. Letters such as é or ß are treated as literal characters, so add several examples or edit the class by hand for international text.
Is anything sent to a server?
No. Patterns are generated and tested in your browser, and your examples are saved in local storage on this device only.
