On Jun 29, David Arnold said: >\backans{If $x=y^{2n}$ and $z=y^{3n}_{11}$, then we can substitute > to find a solution.} > >I'd like to scan the file and replace all of these with this format: > >\begin{answer} >If $x=y^{2n}$ and $z=y^{3n}_{11}$, then we can substitute >to find a solution. >\end{answer}
To match nested things, you probably want to use Regexp::Common, which allows you to do that very easily: use Regexp::Common; $text =~ s< \\ backans { ( $RE{balanced}{-parens=>'{}'} ) } ><\\begin{answer}\n$1\n\\end{answer}>xg; The /x modifier is so that I can have extra whitespace, and the /g modifier means "do it globally". The %RE hash is quite magical -- see the Regexp::Common docs for an explanation. The module isn't standard, though, so you'd have to download it from CPAN yourself. If you want a stand-alone solution, you can have one if you make use of some of Perl's special regex constructs: my $rx; # must be declared first... $rx = qr[ (?: (?> [^{}\\]+ | \\. ) | { (??{ $rx }) } )* ]xs; $text =~ s/\\backans{($rx)}/\\begin{answer}\n$1\n\\end{answer}/g; Its primary trick is the (??{ ... }) assertion, which evaluates its contents as PART of the regex to match. Since its contents are $rx itself, it basically creates an automatically deeply-enough nested regex for you on the fly. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.] <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>