Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Matching delimiters

by pop18 (Novice)
on Dec 19, 2007 at 05:10 UTC ( [id://657792]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
I have a problem in mathcing delimiters. I want to replace
\caption{*} with <caption>*</caption>. I use the following code.

$str=~s#\\caption\{(.[^\}]*)\}#<caption>$1<\/caption>#gis;

But it results in selecting the next immediate close "}" and not finding the last "}".
Please help

Thanks in Advance,
POP

Replies are listed 'Best First'.
Re: Matching delimiters
by educated_foo (Vicar) on Dec 19, 2007 at 05:30 UTC
    perldoc -q matching delimiters is your friend.

    Alternatively, since 5.10 came out today, you can use scary regex features:

    s/\\caption(\{((?:[^\{\}]|(?1))*)\})/<caption>$2<\/caption>/;
Re: Matching delimiters
by vivid (Beadle) on Dec 19, 2007 at 06:01 UTC

    Try this

    use Regexp::Common; use Regexp::Common::balanced; $str=~s#\\caption($RE{balanced}{-begin=>"{"}{-end=>"}"}{-keep})#<capti +on>$1</caption>#sgi;

    Vivid

Re: Matching delimiters
by graff (Chancellor) on Dec 19, 2007 at 05:48 UTC
    I'm not sure I understand what problem you are trying to describe -- the sample input string you showed (\caption{*}) turns into the output you want (<caption>*</caption>) using the code snippet as originally posted. Maybe if you showed the particular input sample that causes a problem for you?

    Anyway, I'd probably write the regex a little differently -- this would behave a bit different from the OP version, in allowing the input to have an empty string (i.e. nothing) inside the curly braces (whereas the OP would not match unless there's at least one character in there):

    s# \\ (caption) \{ ([^}]*) \} #<$1>$2</$1>#gisx;
    That also works on the sample input string you originally posted. If you have a string that doesn't work with that, show us.
Re: Matching delimiters
by lodin (Hermit) on Dec 19, 2007 at 07:54 UTC

    People has already answered your question, so I just wonder if \caption{*} is TeX code? If so, there are CPAN modules for parsing it.

    lodin

      Dear Monks,
      The below is the string that i want to replace,

      \caption{Profit under {\vphantom{q}}boundary equilibrium $\overline {q +}{\vphantom{q}}_{{\mathrm{1}}}^{{\mathrm{BE}}}$. Region of {\it{A}}-s +tability for TSRK methods with $s\equ {\mathrm{1}}$ and $\widetilde { +p}\equ q\equ {\mathrm{2}}$.}
      You see, there are many "}" inside the \caption\{*\}. All the replies above result in
      <caption>Profit under {\vphantom{q}</caption>boundary...
      but i need the closing tag the last "}" as
      <caption>Profit ...\mathrm{2}}$.</caption>.

      Please help.

      Thanks,
      POP
        Dear Monks,
        Sorry, I had a syntax error. The code of VIVID works...

        Thank you very much,
        POP

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://657792]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-25 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found