#!/usr/bin/perl -w use v5.14; no if $] >= 5.017011, warnings => 'experimental::smartmatch'; sub ok{ chomp; # Remove the final newline y/+/-/; # Replace + with - return if /^-|-$|\[-|-\]/; # ERROR if the string starts/ends with a - or if it contains [- or -] 1 while s/\[([^[\]]+)]/-$1/; # Replace [...] with +... my ($base, $index, $vars) = (0, 0, 0); for (split '-'){ # Tokenize the expression, using '-' as the delimiter $base++ when m/^bx$|^bp$/i; # If the current token is bx or bp, increment $base $index++ when m/^si$|^di$/i; # If the current token is si or di, increment $index $vars++ when m/^[a-z]$/; # If the current token is a lowercase letter, increment $vars 1 when m/^\d*$/i; # If the current token is a number, do nothing default { return } # Otherwise ERROR } $base < 2 && $index < 2 && $vars < 2 } say ok() ? 'OK' : 'ERROR' while <>;