#!/usr/bin/perl -w use strict; my $string = "stash_ST1.2.3.4_1.ROLLBACK.check.20170228-101051.435944.txt"; my $action = ''; my @parts = split(/\./, $string); # split on dot for (0..$#parts){ # slot after ROLLBACK $action = $parts[$_+1] if $parts[$_] eq "ROLLBACK"; } print "$action\n";