$string = qq { Test text PDB code ID 1TGS 1O6S 1TGS bla bla PDB code 1ILW 1ILV PDB 2XXX }; while($string=~/PDB (?:code )?(?:ID )?(([1-9][A-Z0-9]{3} ?)+)+/mg){ print $1, "\n";}