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.
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.
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.
Testing happens in your browser: log lines are not sent anywhere.
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.
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.
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.
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.
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 [à-ÿ].
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 .*.