$text = 'A0 peter Z0 ... A42 peter, paul and mary Z42 ... A99 mary Z99'; my @and_matches =( $text =~ m/ A(\d+)[^Z]* ( (?=mary) [^Z]* peter | (?=peter) [^Z]* mary ) [^Z]*Z /xg ); print "@and_matches \n"; __END__ 42 peter, paul and mary