#!/usr/bin/perl use Data::Dumper; my $text = join('', ); my %hash = ($text =~ /(\d+)\~\/g); print Dumper(\%hash); __DATA__ 4~, Some junk 5~, more junk 6~,sd 7~, last junk 99 8~,