Hi, I am not sure that I understand what you want to do, but I am sure that I really don't understand why you are making things so complicated. Please explain what you want to do, or what result you want to obtain.

May be you want to try simply this:

!/usr/bin/perl use warnings; use strict; $_ = "a { b } c ( d ) e"; my $nobrackets = qr/([^{}]+)/; s/\{$nobrackets\}/leftbracket $1 rightbracket/g; print "$_\n";
which prints:
a leftbracket b rightbracket c ( d ) e
which is presumably what you want. Is this right? Or do you have something else in mind?

The would be easier ways to obtain the same result (such as changing the curlies only, rather than a combination of curlies and text), but I tried to stay close to what you have.

Update : Based on your update on Mar 15, 2014 at 05:33 UTC, I now understand that you had this complicated way of doing things simply because you wanted to do the replacement in a subroutine. You might modify the above in the following way:

use warnings; use strict; my @c = ("a { b } c ( d ) e", "a { b } c ( d ) e {{F}} ((G))"); print replace($_), "\n" for @c; sub replace { my $d = shift; my $nobrackets = qr/([^{}]+)/; $d =~ s/\{$nobrackets\}/leftbracket $1 rightbracket/g; return $d; }
which gives the following output:
a leftbracket b rightbracket c ( d ) e a leftbracket b rightbracket c ( d ) e {leftbracket F rightbracket} +((G))


In reply to Re: Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements) by Laurent_R
in thread Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements) by JDoolin

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.