Regular expressions and parenthesis in match text

Discussion in 'Perl Misc' started by Carl Cunningham, Sep 15, 2003.

  1. Hi folks,

    I'm going bonkers with what should be a simple problem. I'm trying to change
    this text (don't worry about <ul> & </ul>, I've got that taken care of):


    <p>Bla bla</p>
    <p>- dis dat (hjkhjk) </p>
    <p>- DIS DAT</p>
    <p>Bla bla</p>

    into:

    <p> Bla bla</p>
    <li>- dis dat </li>
    <li>- DIS DAT</li>
    <p> Bla bla</p>

    This script seems to work fine (keep an eye on the parenthesis):
    -------------------------------------
    $text = "<p>Bla bla</p>
    <p>- dis dat </p>
    <p>- DIS DAT</p>
    <p>Bla bla</p>
    " ;

    @a = ($text =~ m/(<p>-.*?<\/p>)/sg) ;

    foreach $temp (@a) {
    $temp2 = $temp ;
    $temp =~ s/p>-|p>/li>/g ;
    $text =~ s"$temp2"$temp" ;
    }
    print $text ;
    -------------------------------------

    But if the text to be matched contains parenthesis:

    $text = "<p>Bla bla</p>
    <p>- dis dat (hjkhjk) </p>
    <p>- DIS DAT</p>
    <p>Bla bla</p>
    " ;

    The "m/expression/" matches the desired lines, but the s/// operator doesn't
    match the line containing the parenthesis.

    Could somebody puh-leaze help me on this?


    ----------------------------------------------------------------------------
    Carl K. Cunningham
    RobertsĀ“ interactive GmbH
    E-Mail: initials of first and last name, at 'roberts' dot 'de'
    ----------------------------------------------------------------------------
    Carl Cunningham, Sep 15, 2003
    #1
    1. Advertising

  2. Carl Cunningham wrote:

    > Hi folks,


    <SNIP>

    > This script seems to work fine (keep an eye on the parenthesis):
    > -------------------------------------
    > $text = "<p>Bla bla</p>
    > <p>- dis dat </p>
    > <p>- DIS DAT</p>
    > <p>Bla bla</p>
    > " ;
    >
    > @a = ($text =~ m/(<p>-.*?<\/p>)/sg) ;
    >
    > foreach $temp (@a) {
    > $temp2 = $temp ;
    > $temp =~ s/p>-|p>/li>/g ;
    > $text =~ s"$temp2"$temp" ;
    > }
    > print $text ;
    > -------------------------------------
    >
    > But if the text to be matched contains parenthesis:
    >
    > $text = "<p>Bla bla</p>
    > <p>- dis dat (hjkhjk) </p>
    > <p>- DIS DAT</p>
    > <p>Bla bla</p>
    > " ;
    >
    > The "m/expression/" matches the desired lines, but the s/// operator
    > doesn't match the line containing the parenthesis.
    >
    > Could somebody puh-leaze help me on this?


    You have just to make perl to quote the meta characters for you with \Q
    Change

    $text =~ s"$temp2"$temp" ;

    to

    $text =~ s"\Q$temp2"$temp";

    It will work fine with that.
    Patrick de Palma, Sep 15, 2003
    #2
    1. Advertising

  3. in Beitrag bk44ul$p8ai3$-berlin.de schrieb Patrick de
    Palma unter am 15.09.2003 12:40 Uhr:

    >
    > You have just to make perl to quote the meta characters for you with \Q
    > Change
    >
    > $text =~ s"$temp2"$temp" ;
    >
    > to
    >
    > $text =~ s"\Q$temp2"$temp";
    >
    > It will work fine with that.



    Doh! Thank you _very_ much.



    ----------------------------------------------------------------------------
    Carl K. Cunningham
    RobertsĀ“ interactive GmbH
    E-Mail: initials of first and last name, at 'roberts' dot 'de'
    ----------------------------------------------------------------------------
    Carl Cunningham, Sep 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    407
    George Sakkis
    Jul 13, 2005
  2. Daniel Fac
    Replies:
    3
    Views:
    134
    Daniel Fac
    Nov 2, 2008
  3. Bob
    Replies:
    11
    Views:
    354
    David Combs
    Jul 13, 2004
  4. PerlFAQ Server
    Replies:
    0
    Views:
    109
    PerlFAQ Server
    Jan 9, 2011
  5. Noman Shapiro
    Replies:
    0
    Views:
    219
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page