use strict; use warnings; my $test_string = "3 xyz"; # Start a global search # It looks like the expected pattern includes a space. Let's put it here. if ( $test_string =~ m/^(\d+) /g ) { my $length = $1; # Pick up the search where our first match left off. if ( $test_string =~ m/\G(.{$length})/ ) { my $result = $1; # Do stuff with $result ... } }