#!/usr/bin/perl use strict; use warnings; my $string =<<"HERE"; I have some > and > > and that's it. HERE my @groups = $string =~ m/(<(?:[^<>]++|(?1))*>)/g; $" = "\n\t"; print "Found:\n\t@groups\n";