/([QGYN]{2} # First two characters of the desired class (?: # Followed by the complex expression... # Look for a trio starting two characters back (?<=(?!(.)\2\2)..) [QGYN] # Then take another of the desired class ){1,4} # ...1 to 4 times )/gx