$ cat t.pl #!/usr/bin/perl use strict; use warnings; local $/ = '?srt='; while () { chomp; print "$1\n" if m[-/(\w{5,20})$]; } __DATA__ ...emable-Stuff-10100-PTZ-/1280640AB018292?srt=More2stuff&ha... ...wer-Idaptx-SJ10-/35DE4715844?srt=L12_Defa43Dom.. foo-/a?srt=bar;foo-/abcdefghijklmnopqrstuvwxyz?srt= foo-/abcde?srt=bar-/abcdef?srt=baz-/abcdefg?srt= $ perl t.pl 1280640AB018292 35DE4715844 abcde abcdef abcdefg