You don't need to escape the } within a bracketed character class. You do need not to accept any opening { within your bracketed character class.
use Data::Dumper; my $string = 'some text ###RAND{1a|###RAND{2a|2b|2c|###RAND{3a|3b|3c|3 +d}|2d}|1b|1c|1d} some text'; print process_rands($string)."\n"; sub process_rands { my $sentence = shift; while (1) { last unless $sentence =~ s!###RAND\{([^{}]+)\}!get_rand_arr_ +el($1)!e; print $sentence."\n"; } return $sentence; } sub get_rand_arr_el { warn Data::Dumper->Dump([\@_],[qw(*_)]),' '; my @array = split '\|',$_[0]; my $randomelement = $array[ rand @array ]; return $randomelement; }
yields
@_ = ( '3a|3b|3c|3d' ); at play.pl line 18. some text ###RAND{1a|###RAND{2a|2b|2c|3c|2d}|1b|1c|1d} some text @_ = ( '2a|2b|2c|3c|2d' ); at play.pl line 18. some text ###RAND{1a|2b|1b|1c|1d} some text @_ = ( '1a|2b|1b|1c|1d' ); at play.pl line 18. some text 1d some text some text 1d some text

In reply to Re^3: Regex for replacing multiple nested matches by clueless newbie
in thread Regex for replacing multiple nested matches by Lana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.