my $row = 'We_need_feed'; #my ($last) = $row =~ /[^_]$/g; #match we # [^_] matches everything except underscore, but only one character; # Plus you've anchored it to the last character before the end of the string, # so it will always and only match the last character of the string; # unless it is an underscore, in which case it fails to match at all. #my ($last) = $row =~ /^\w?[^_]/g;#match feed # ^\w? matches a single non-whitespace character at the start of the string, # if the first character isn't whitespace, otherwise it matches nothing. # [^_] matches a single non-underscore as above. # Ie. You matched two characters "We" my ($last) = $row =~ /[need]+$/g ; # [need]+ matches a one or more characters, so long as they are either 'd' or 'e' or 'n'; other wise nothing # but the $ means only at the of the line. # so this matched the 3-char string 'eed'.