Regex tester

Check a pattern against log lines before you put it on a server.
fail2ban, nginx, grep, Python and JavaScript — with a breakdown and a ready command.

Regex tester

Check a pattern against log lines before you put it into fail2ban, nginx or a script. The tester knows the quirks of each: the <HOST> and <F-USER> tags in fail2ban, [[:digit:]] classes and plain grep without -E, Python named groups. Matches are highlighted, the pattern is explained piece by piece, and below you get a ready command or config snippet.

Result

Pattern breakdown

Ready command


   

    Testing happens in your browser: log lines are not sent anywhere.

    One pattern, different rules

    Regular expressions look alike everywhere, but details differ, and because of them a rule silently fails. Plain grep treats “+” and “|” as ordinary characters, grep -E does not know \d or lazy repeats, and inside grep's square brackets a backslash is an ordinary character. fail2ban writes patterns in Python, cuts the date from the line itself and requires the <HOST> tag, and the % sign has to be doubled in its files. nginx compares location with the path without parameters. The tester takes these differences into account for the chosen place and warns you when the pattern will behave differently there than you expect.

    How to use it

    1. Choose where the pattern will run: fail2ban, nginx, grep, Python or JavaScript. Or take a ready example — SSH, basic auth and limit_req in nginx, scanners, Asterisk, static files, User-Agent.
    2. Paste the pattern and a few lines from a real log — ideally both lines that should match and lines that should not.
    3. Look at the highlighting: which lines matched, what went into groups, which IPs fail2ban will ban. If the result is wrong, open “Pattern breakdown” — it shows what each part means.
    4. Copy the ready command or config snippet. Before enabling a fail2ban filter, test it with fail2ban-regex on the server.

    What else helps

    Ready jails for SSH, nginx, mail and Asterisk are built by fail2ban setup, and log analysis finds password guessing and scanners in logs by itself — it shows which lines are worth catching. A site configuration with limits and the right order of locations is made by the nginx config generator.

    Further reading

    Frequently asked questions

    Why does the pattern work in the tester but fail2ban finds no matches?

    Most often because of the date: fail2ban cuts it from the line before checking, so a pattern with ^ and a date will not match, and in access.log “[]” remains where the date was. Keep “Cut the date, as fail2ban does” ticked. Other reasons are an undoubled % in the filter file and a different log in logpath. The final check is fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/name.conf.

    What is the difference between grep, grep -E and grep -P?

    Plain grep understands basic expressions: “+”, “?”, “|”, parentheses and braces must be written with a backslash. grep -E uses extended ones: they are written without the backslash, but \d, lazy repeats and (?…) do not exist there. grep -P uses Perl and PCRE expressions, with \d, (?:…) and lookahead and lookbehind; it exists in GNU grep on Linux.

    Why doesn't \w find Cyrillic or accented letters?

    It depends on the program and the locale. grep in a UTF-8 locale and Python 3 count them as letters, while JavaScript and many PCRE builds without the Unicode flag do not. If you need such letters, it is more reliable to list the range explicitly, for example [а-яёА-ЯЁ] or [à-ÿ].

    What is catastrophic backtracking?

    It is when a pattern with nested repeats, such as (a+)+ or (.*,)*, tries a huge number of variants on a line that almost fits. Checking one line can take minutes, and nginx or fail2ban CPU load goes up. The tester stops such a check after a couple of seconds. Remove the nested repeat or make the parts mutually exclusive: [^,]* instead of .*.

    More tools

    Utilities

    Random number generator
    Any range, no repeats, lottery and dice
    Random picker
    Winner, teams, order and a coin
    Cron
    Explain cron and OnCalendar, next runs
    chmod
    Permissions: 755 ↔ rwxr-xr-x, umask
    Converters
    Unix time, Base64, hex, URL and Punycode
    JWT
    Decode a token and check its signature
    Checksums
    SHA-256, SHA-512, MD5 of files locally
    Photo metadata
    GPS, camera and AI data: view and remove