use Text::Balanced; my $text = 'sometexthere'; ($extracted, $remainder) = Text::Balanced::extract_tagged($text);