qr { ^ # Anchor at the start _? # Optional underscore [\#*]* # Optional sharps and stars # Numerical part: [0-9]+ # Digits (?: # Optional \[\s*[0-9]+-[0-9]\] # '[', whitespace, digits, '-', digit, ']' )? # End optional [\#*]* # Optional sharps and stars # Character part: X* # Zero or more 'X' (?: # Optional # Either !X* # '!' followed by 'X's, (?:\.X*)? # optionally '.' followed by 'X's | # or \.X* # '.' followed by 'X's, (?:!X*)? # optionally '!' followed by 'X's )? # End optional $ # Anchor at the end. }x;