my $re= qr{\[\[ # opening delimiter ( # capture... (?: # a group (?!\]\]) # that does not start with ]] . # and is a single character long )+ # and many of these groups, at least one. ) \]\] # closing delimiter }x; $section_contents =~ s{$re}{Process_Tag($1,$hidden_query,$tag_cfg,$is_preview)}ge; # process stuff like [[1234]] or [[http://www.site.com]]