#!/usr/bin/perl -w use strict; # Slurp the input into $data my $data; { local $/; $data = ; }; # This is the naive way, using the non-greedy .*? if ($data =~ /:::(.*?):::/ms) { print "$1\n"; } else { print "No match.\n"; }; # This is the "perfect" way (should be described in the Owl book # somewhere). It's much more specific about what it wants, and # thus longer and more complex :-) if ($data =~ /::: # start ([^:]* # As many non-: as we can gobble (?: ::?[^:]+ # and then one or two :'s as long as they are )* # followed by something non-: ) ::: # end /msx # And we want to match spanning lines # and use eXtended re syntax ) { print "$1\n"; } else { print "No match.\n"; }; __DATA__ Some foo:::This is a some text. Today you watered the dog and ::test: bathed the plants. The server asked you what permission you had to tell it what to do on it's day off. This was your day.::: More foo.