Use contents of variable for search and replace

W

wappel

I have the following problem:

I have an XML text which consists of several chapters that have a
number-Attribute
<xmltext>
<chapter number="345">
...searchtext...
</chapter>
<chapter number="446">
...searchtext...
</chapter>
</xmltext>

Now I want to replace the ...searchtext... by <search
number="xxx">...searchtext...</search>, where xxx is the value of
number-Attribute of the chapter in which the ...searchtext... stands:
<chapter number="345>
<search number="345">...searchtext</search>
<chapter>
<chapter number="446">
<search number="345">...searchtext</search>
</chapter>
...searchtext... could be a regular expression.

Maybe it is possible to do it with
s/(...searchtext...)/<search number="$variable">$1</search>/gs;
???
But how do I get the right value into the variable?


Thanks for your help

Wolfgang
 
B

Ben Morrow

Quoth (e-mail address removed) (wappel):
I have the following problem:

I have an XML text which consists of several chapters that have a
number-Attribute
<xmltext>
<chapter number="345">
...searchtext...
</chapter>
<chapter number="446">
...searchtext...
</chapter>
</xmltext>

Now I want to replace the ...searchtext... by <search
number="xxx">...searchtext...</search>, where xxx is the value of
number-Attribute of the chapter in which the ...searchtext... stands:
<chapter number="345>
<search number="345">...searchtext</search>
<chapter>
<chapter number="446">
<search number="345">...searchtext</search>
</chapter>
..searchtext... could be a regular expression.

Maybe it is possible to do it with
s/(...searchtext...)/<search number="$variable">$1</search>/gs;
???
But how do I get the right value into the variable?

Use an XML-parsing module: try XML::Simple or XML::Twig.

Ben
 
P

Paul Lalli

I have the following problem:

I have an XML text which consists of several chapters that have a
number-Attribute
<xmltext>
<chapter number="345">
...searchtext...
</chapter>
<chapter number="446">
...searchtext...
</chapter>
</xmltext>

Now I want to replace the ...searchtext... by <search
number="xxx">...searchtext...</search>, where xxx is the value of
number-Attribute of the chapter in which the ...searchtext... stands:
<chapter number="345>
<search number="345">...searchtext</search>
<chapter>
<chapter number="446">
<search number="345">...searchtext</search>
</chapter>
..searchtext... could be a regular expression.

Maybe it is possible to do it with
s/(...searchtext...)/<search number="$variable">$1</search>/gs;
???
But how do I get the right value into the variable?

Ideally, you would be far far better off using one of the XML Parsing
modules on CPAN. Failing that, however, this would be my initial
(untested) attempt:

s[(<chapter number="(\d+)">)(.*?)(</chapter>)]
[$1\n<search number="$2">$3</search>\n$4]gs;

Again, however, it would be far preferred to use one of the modules
specifically designed for this task.

Paul Lalli
 
B

Ben Morrow

Quoth Paul Lalli said:
I have the following problem:

I have an XML text which consists of several chapters that have a
number-Attribute
<xmltext>
<chapter number="345">
...searchtext...
</chapter>
<chapter number="446">
...searchtext...
</chapter>
</xmltext>

Now I want to replace the ...searchtext... by <search
number="xxx">...searchtext...</search>, where xxx is the value of
number-Attribute of the chapter in which the ...searchtext... stands:
<chapter number="345>
<search number="345">...searchtext</search>
<chapter>
<chapter number="446">
<search number="345">...searchtext</search>
</chapter>
..searchtext... could be a regular expression.

Maybe it is possible to do it with
s/(...searchtext...)/<search number="$variable">$1</search>/gs;
???
But how do I get the right value into the variable?

Ideally, you would be far far better off using one of the XML Parsing
modules on CPAN. Failing that, however, this would be my initial
(untested) attempt:

s[(<chapter number="(\d+)">)(.*?)(</chapter>)]
[$1\n<search number="$2">$3</search>\n$4]gs;

Presumably the OP (although it isn't clear) didn't mean the searchtext
to be the whole contents of the <chapter>. So (also untested):

my @chapters;
my $searchtext = qr/.../;

while (length $xml) {
$xml =~ s/(.+)(?!<chapter\s)//s and push @chapters, [undef, $1];
push @chapters, [$1, $2]
while $xml =~ s!^<chapter\s+number="(\d+)">(.*?)</chapter>!!s;
}

for (@chapters) {
defined $_->[0] or $xml .= $_->[1], next;
$_->[1] =~ s{($searchtext)}
{<search number="$_->[0]">$1</search>}g;
$xml .= "<chapter number="$_->[0]">$_->[1]</chapter>";
}

Again, it would *really* be better to use an XML parsing module.

Ben
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top