Regular expressions and parenthesis in match text

C

Carl Cunningham

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?
 
P

Patrick de Palma

Carl said:
Hi folks,

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.
 
C

Carl Cunningham

in Beitrag [email protected] schrieb Patrick de
Palma unter (e-mail address removed) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top