DanielNeedles has asked for the wisdom of the Perl Monks concerning the following question:

Is there a cleaner way via a regex to recurse through a series of '}' at the end of a line and insert newlines?

Specifically can:
my $bracketend = 8 * $lines =~ s/\}\s*\}\s*\}\s*\}\s*\}<\s*\}\s*\}\s*\ +}$/\n\}\n\}\n\}\n\}\n\}\n\}\n\}\n\}/gms; $bracketend += 4 * $lines =~ s/\}\s*\}\s*\}\s*\}$/\n\}\n\}\n\}\n\}/gms +; $bracketend += 2 * $lines =~ s/\}\s*\}$/\n\}\n\}/gms; $bracketend += $lines =~ s/\}$/\n\}/gms;
Get replaced by a single line of regex that recurses through text that contains many instances of /}+$/ and convert each '}' to \n}

Replies are listed 'Best First'.
Re: Cleaner regex conversion
by pc88mxer (Vicar) on Jul 05, 2008 at 06:54 UTC
    How about giving us an example of what you want to do?

    In the mean time, here's a stab at what I think you mean:

    my $lines = "} } \n } }\n foo } }\n"; $lines =~ s,(}[}\s]*)\n, "}\n" x ($1 =~ tr!}!!) ,xgem; print $lines; __END__ } } } } foo } }
      This is perfect. I haven't seen the format before this:

      s/()/"." x ()/ge

      Is there a URL or book that covers these more advanced regexp?

        Well, the official perl book is 'Programming Perl' (Wall,Christiansen,Schwartz) and it naturally has this and practically everything else covered.

        Also check out the manual pages, in this case perlre (also available online: http://perldoc.perl.org/perlre.html ).