BL2.ru

Regular expression tester (regex online)

An online tester for PHP regular expressions (PCRE): enter a pattern and text and see what preg_match and preg_match_all find, or what preg_replace produces. Handy for debugging patterns before they get into the code.

Delimiters

In PHP a pattern is written between delimiters: usually /…/, but #…#, ~…~ or {…} work too. Modifiers follow the closing delimiter. If a pattern has many slashes (paths, URLs), another delimiter is easier: #^/catalog/(\d+)/$# instead of escaping every /.

Modifiers

ModifierWhat it doesExample
icase-insensitive/abc/i matches "ABC"
mmultiline: ^ and $ match at every line/^\d+$/m
sdot also matches a newline/<p>.*<\/p>/s
xignore whitespace and # comments in the pattern/ \d{3} # code /x
upattern and subject are UTF-8 (required for Cyrillic)/[а-яё]+/iu

The e modifier (evaluating code in the replacement) was removed in PHP 7 and is stripped on this page for security — use preg_replace_callback().

Common patterns

TaskPattern
Digits only/^\d+$/
All numbers in text/\d+/
Email-ish/^[^@\s]+@[^@\s]+\.[a-z]{2,}$/i
Russian phone +7XXXXXXXXXX/^\+7\d{10}$/
Date DD.MM.YYYY/^(\d{2})\.(\d{2})\.(\d{4})$/
Repeated spaces/\s+/

For example, preg_match_all('/\d+/', 'a1b22c333') finds three matches: 1, 22, 333, and preg_replace('/\s+/', ' ', $s) collapses any spaces and newlines into one space.

preg_match, preg_match_all, preg_replace

  • preg_match — finds the first match and returns 1 or 0; groups go to $matches. Good for format validation.
  • preg_match_all — finds all matches and returns their count. Good for extracting data: all links, numbers, emails.
  • preg_replace — replaces matches; groups are available in the replacement as $1 or \1: preg_replace('/(\d{2})\.(\d{2})\.(\d{4})/', '$3-$2-$1', '06.10.2026') → 2026-10-06.

Encoding: cp1251 vs utf-8

PCRE works on bytes. In UTF-8 a Cyrillic letter is 2 bytes, so without the /u modifier classes like [а-я], the dot and \w misbehave and may cut a letter in half. Rule: always add u for UTF-8 text. In cp1251 a letter is one byte and patterns work without u, but characters outside cp1251 (emoji, CJK) cannot be represented. Choose the same encoding on the page as in your project.

How to use the page

  • Choose the function (preg_match_all, preg_match or preg_replace) and the encoding.
  • Enter the regular expression with delimiters and modifiers and, if needed, the replacement.
  • Paste the text and press "Execute": matches, groups or the replacement result appear.