Test a regular expression

test a regular expression (regex) against a text and display matches and captured groups

What is a regex tester for?

A regex tester lets you check that a regular expression (regex) pattern indeed matches the expected text, without running application code. You paste the pattern, you paste the input text, you tick the options (flags), and the tool immediately displays the list of matches found as well as the captured groups. It is the online equivalent of preg_match_all() in PHP, of String.matchAll() in JavaScript or re.findall() in Python.

Regexes are both powerful and treacherous. A comma, a misplaced parenthesis, a greedy quantifier that sucks up too many characters: and the extracted string is no longer the expected one. The tester avoids the back-and-forth between an editor, a terminal and a test file: you iterate the pattern until you see the right result.

How do I write the pattern?

Enter the regex without delimiters. No /.../, no #...#. For example, to capture email addresses from a text, enter only [\w.+-]+@[\w-]+\.[\w.-]+. Options (flags) are controlled by the checkboxes.

The u (Unicode) option is ticked by default. It is almost always wanted: without it, \w does not recognise accents, and some patterns return a silent PCRE error on UTF-8 strings.

The PCRE options

  • i (case-insensitive): [a-z] also recognises uppercase.
  • m (multiline): ^ and $ match the starts and ends of each line, not just the start and end of the whole string.
  • s (dotall): . also recognises line breaks. Useful for patterns that need to span paragraphs.
  • u (unicode): interprets the pattern and the subject in UTF-8. Essential as soon as the text contains non-ASCII characters (accents, emoji, ideographs).

Common regex examples

  • Email address: [\w.+-]+@[\w-]+\.[\w.-]+ (simplified form, sufficient for most practical cases; strict RFC 5322 is very complex).
  • HTTP/HTTPS URL: https?://[\w.-]+(?:/[\w./?%&=-]*)?
  • French phone number: (?:\+33|0)\s?[1-9](?:[\s.-]?\d{2}){4}
  • French postcode: \b\d{5}\b
  • ISO date (YYYY-MM-DD): \b\d{4}-\d{2}-\d{2}\b
  • IPv4 address: \b(?:\d{1,3}\.){3}\d{1,3}\b
  • Whole word: \bfoo\b (the word boundaries avoid matching football).
  • Hashtag: #\w+
  • Semantic version number: \d+\.\d+\.\d+(?:-[\w.-]+)?
  • Twitter / X handle: @\w{1,15}

Captured groups

A regex can contain parenthesised groups, which isolate a portion of the match for reuse. For example, the pattern (\w+)@(\w+\.\w+) applied to alice@example.com captures two groups: alice and example.com. The tool displays these groups in a dedicated column, next to each match. Named groups ((?P<name>...)) are also listed, indexed by their name.

To group without capturing (useful to apply a quantifier without polluting the list of groups), use (?:...).

ReDoS protection

PHP's PCRE engine can enter catastrophic backtracking on some pathological patterns: a pattern like (a+)+$ applied to a long string of a that never ends can block the server for several seconds. To avoid this regular-expression denial of service (ReDoS), the tool applies two limits:

  • a set_time_limit(2) on the worker side (processing gives up after 2 seconds);
  • a reduction of pcre.backtrack_limit to 100,000, which makes patterns that explore too many combinations fail quickly.

For a too-costly pattern, the tool displays the PCRE error message (preg_last_error_msg()), typically Backtrack limit was exhausted. Rewrite your pattern using possessive quantifiers (a++) or atomic groups ((?>...)) to avoid backtracking.

Frequently asked questions

Why does my regex not match while it works in JavaScript?

This tool's engine is PCRE (PHP), close to Perl. The syntax is very similar to JavaScript but a few features differ: variable-length lookbehinds, some Unicode aliases, or the default case-sensitivity handling. If you plan to run the pattern in the browser, also test in a JS environment.

Why does my \w not capture accents?

Without the u flag, \w only recognises [A-Za-z0-9_]. With u (Unicode), it recognises all letters in the UCD sense, including accented characters. To go further, use Unicode classes: \p{L} (letter), \p{N} (digit), \p{P} (punctuation).

Does the tool keep my text?

No. The pattern and the subject are processed in RAM by the PHP worker, with no persistence. No submitted data is logged.

What if I get a "Backtrack limit was exhausted" message?

Your pattern does too much backtracking. Look for nested quantifiers like (a+)+ or (\w+)*, which are typical ReDoS cases. Replace them with an unambiguous form: for example \w+ instead of (\w+)*.

Can I test a multi-line pattern?

Yes. Tick the m option so that ^ and $ recognise the start and end of each line. Also tick s if you want . to span line breaks.

Example request

curl -X POST https://cdrn.fr/api/v1/tools/regex-tester/execute \
  -H "Content-Type: application/json" \
  -d '{"pattern":"...","subject":"...","flag_i":false,"flag_m":false,"flag_s":false,"flag_u":true}'

Input schema

Field Type Required Default
pattern string
subject text
flag_i boolean false
flag_m boolean false
flag_s boolean false
flag_u boolean true

Endpoints

  • GET https://cdrn.fr/api/v1/tools - lists every available tool
  • GET https://cdrn.fr/api/v1/tools/regex-tester - returns the schema for this tool
  • POST https://cdrn.fr/api/v1/tools/regex-tester/execute - runs this tool with a JSON payload