$ cat t.pl use strict; use warnings; my @examples = ( 'Something something -something1 something eid- 1234 gkn 12-34_loanmaster', 'Something :something something6 eid - 4532 gkn 34-21-hostmasfer', 'eid 762 something something1 something@', ); for my $v (@examples) { if ($v =~ / eid # Prefix for the ID \s* # might have some spaces (-\s*)? # maybe a hyphen with more spaces (\d+) # has one or more digits /x) { print "Found ID <$2> in <$v>\n"; } } $ perl t.pl Found ID <1234> in Found ID <4532> in Found ID <762> in