#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11167676 use warnings; local $_ = 'AB ABDA DCACCB AAA BSAA CAAB ACS ABA DBA BA DASSABACA A'; my $first = /(BA[ABC]{2})|(CA[CD]{2})|DA[SC]{2}/ ? $1 ? 1 : $2 ? 2 : 3 : 0; print "first is $first\n"; #### first is 2