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

Hi all, I am trying to extract the part of a string from a main string. ex : <page>He is a 'strong'good and bad'/strong' boy </page> I want to extract the text between the 'strong''/strong' into a variable and replace the 'strong''/strong' by Blanks. Note : inverted commas are there so that the tag is not converted in Bold.You can replace the inverted commas with < or >

Replies are listed 'Best First'.
Re: Extract substring from a string
by gone2015 (Deacon) on Aug 26, 2008 at 12:41 UTC

    Learning how to parse the XML, or whatever it is, will serve you well as your needs grow.

    On a quick and dirty basis:

    $p = $s =~ s{\<strong\>(.*?)\</strong\>}{ $1 } ? $1 : undef ;
    should replace any <strong>....</strong> by ' .... ' in $s, and set $p to be a copy of the '....'.

    Mind you, this leaves as an exercise:

    1. how you read stuff into $s
    2. if reading line by line, how to deal with <strong>....</strong> sections that span one or more lines
    3. in any event, how to deal with pesky CRLF sequences, if there are any
    4. whether changing foo<strong>FOO</strong>bar to foo FOO bar is really what you want -- that is, do you want to introduce space(s) where there were none before ?
    5. how any nested <strong>..</strong> sections are to be handled -- which the fragment above fails on, utterly
    Some, or all, of which may be dealt with by biting the bullet and parsing the thing.

Re: Extract substring from a string
by apl (Monsignor) on Aug 26, 2008 at 11:39 UTC