Testeur Regex

Tester, faire correspondre, remplacer et diviser le texte en utilisant des expressions régulières avec une validation en temps réel

Modèle d'expression régulière

//gValide

Strime de test

Personnages: 0Lignes: 1

Résultat

Référence rapide regex

Classes de personnages

\\d - Digits (0-9)
\\w - Word characters
\\s - Whitespace
. - Any character

Quantificateurs

* - 0 or more
+ - 1 or more
? - 0 or 1
{n,m} - Between n and m

Ancres

^ - Start of string
$ - End of string
\\b - Word boundary
\\B - Non-word boundary

Regex Tester - Test Regular Expressions Online

An online regex tester helps you design, debug, and explain regular expressions with instant feedback. Use it to validate email addresses, extract IDs, normalize whitespace, parse logs, or rewrite text with safe replacements. With clear examples and real‑time highlighting, you can learn core concepts—character classes, groups, quantifiers, anchors, lookarounds—without memorizing every symbol. This page focuses on the practical use cases developers hit every day and shows how to avoid performance pitfalls while keeping patterns readable and maintainable.

Regex basics: patterns, flags, and character classes

A regular expression describes sets of strings using literals and metacharacters. Common elements: a dot (.) matches any character, anchors (^) and ($) match start and end, quantifiers (* + ? {m,n}) control repetition, character classes ([a-z], \d, \w, \s) match categories, groups (( ... )) capture substrings, and alternation (a|b) chooses among options. Flags like g (global), i (ignore case), m (multiline), s (dotall), and u (Unicode) change how searches run. Together, these let you express powerful matchers for validation and text processing.

Practical patterns you can adapt immediately

Email (pragmatic UI check): ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ handles common addresses without heavy RFC edge cases. URL (basic screen): ^https?:\/\/[^\s/$.?#].[^\s]*$ filters http/https links for quick triage. ISO date: ^\d{4}-\d{2}-\d{2}$ matches YYYY‑MM‑DD. Whitespace normalization: replace \s{2,} with a single space globally. Extract issue IDs: /(PROJ-\d+)/ captures ticket keys from commit messages. Treat these as starting points—refine to your domain and add tests for real samples.

Step‑by‑step: build, test, and refine

Start with a concrete example string. Add literal characters first, then generalize with character classes and quantifiers. Introduce anchors to restrict where matches occur, and use capture groups to extract the parts you need (for example, the domain from an email or the path from a URL). In the tester, inspect matches and numbered groups; toggle flags to explore case sensitivity and multiline behavior. If a pattern over‑matches, make a quantifier lazy (.*?) or add a more specific class. If it under‑matches, relax the class or remove unnecessary anchors. Iterate until you get stable, explicit behavior.

Boundaries, lookarounds, and Unicode

Word boundaries (\b) are handy, but not all engines treat them as Unicode‑aware. Use the u flag where supported and prefer explicit classes when working with international text. Lookaheads (?=...) and lookbehinds (?<=...) assert context without consuming characters—great for matching digits only when preceded by a currency symbol, or extracting a slug only when it follows a known prefix. Many engines support lookaheads widely; lookbehinds can be missing in older runtimes, so verify in your target environment.

Performance and catastrophic backtracking

Nested, ambiguous quantifiers can cause catastrophic backtracking, where the engine tries exponential paths before failing. Classic footguns include patterns like (a+)+ on long strings, or (.*)+ combined with alternation. To keep regexes fast: (1) be specific with character classes, (2) avoid nested star/plus quantifiers, (3) prefer atomic groups (?>...) or possessive quantifiers where available, and (4) anchor patterns to reduce search space. Test with large worst‑case strings in the tester to surface slow cases before production.

Search‑and‑replace safely

For rewrite jobs, test capture groups and backreferences thoroughly. For example, to swap last and first name in 'Doe, Jane', use ^([^,]+),\s*(.+)$ with replacement $2 $1. When replacing in code, escape replacement strings correctly—different languages treat $1, \1, or named groups differently. For multi‑line edits, enable the multiline flag and use explicit anchors for lines (^) and ($). Always keep a backup of original text for bulk operations.

Testing strategy and maintainability

Treat regexes as code: write a few representative positive and negative examples, add them to unit tests where patterns are critical, and comment tricky parts. Prefer clarity over cleverness—longer but explicit classes often outperform terse, ambiguous constructs. If a pattern encodes business rules (for example, product SKU formats), document the intent alongside the expression so future maintainers can update it safely.

Further reading

Use the regex tester to iterate safely, document patterns with concrete examples, and keep expressions as simple and specific as possible. Favor clear character classes, anchored searches, and measured use of lookarounds to balance correctness with speed.