Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > second substitution to work only on a found pattern

Reply
Thread Tools

second substitution to work only on a found pattern

 
 
I.M. Postor
Guest
Posts: n/a
 
      10-09-2006
Hello,

I have some xml which is formatted bij a xsl processor:


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file">
<unitid>13</unitid>
<unittitle>
<unitdate>1928</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>14</unitid>
<unittitle>
<unitdate>1929</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>15</unitid>
<unittitle>
<unitdate>1930</unitdate>
</unittitle>
</c03>
</c02>



but whenever there is a <c0X level="file"> element, for working
purposese i'd rather have flattened components:


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file"><unitid>13</unitid><unittitle><unitdate>1928</unitdate></unittitle></c03>
<c03 level="file"><unitid>14</unitid><unittitle><unitdate>1929</unitdate></unittitle></c03>
<c03 level="file"><unitid>15</unitid><unittitle><unitdate>1930</unitdate></unittitle></c03>
</c02>


<XXX level="file"> could be from <c01 level="file"> to <c12 level="file">, therefore:


while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
{ print "hello"; } #OK

while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
{$1 =~ s/\n *//g; }
ERROR: Modification of a read-only value attempted


How can I get the secondary substitution to work only on a found regex?
Or should I try another approach?


Cheers
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      10-09-2006
I.M. Postor wrote:
>
> I have some xml which is formatted bij a xsl processor:
>
>
> <c02 level="group">
> <did>
> <unitid>13-16</unitid>
> <unittitle>several weeklies</unittitle>
> <unitdate normal="1928/1931">1928-1931</unitdate>
> <unitid>13-16</unitid>
> </did>
> <c03 level="file">
> <unitid>13</unitid>
> <unittitle>
> <unitdate>1928</unitdate>
> </unittitle>
> </c03>
> <c03 level="file">
> <unitid>14</unitid>
> <unittitle>
> <unitdate>1929</unitdate>
> </unittitle>
> </c03>
> <c03 level="file">
> <unitid>15</unitid>
> <unittitle>
> <unitdate>1930</unitdate>
> </unittitle>
> </c03>
> </c02>
>
>
>
> but whenever there is a <c0X level="file"> element, for working
> purposese i'd rather have flattened components:
>
>
> <c02 level="group">
> <did>
> <unitid>13-16</unitid>
> <unittitle>several weeklies</unittitle>
> <unitdate normal="1928/1931">1928-1931</unitdate>
> <unitid>13-16</unitid>
> </did>
> <c03 level="file"><unitid>13</unitid><unittitle><unitdate>1928</unitdate></unittitle></c03>
> <c03 level="file"><unitid>14</unitid><unittitle><unitdate>1929</unitdate></unittitle></c03>
> <c03 level="file"><unitid>15</unitid><unittitle><unitdate>1930</unitdate></unittitle></c03>
> </c02>
>
>
> <XXX level="file"> could be from <c01 level="file"> to <c12 level="file">, therefore:
>
>
> while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
> { print "hello"; } #OK
>
> while ($slurped_text =~ /(<(c0[1-9]|c1[012]) level="file">.*?<\/\2>)/sg)
> {$1 =~ s/\n *//g; }
> ERROR: Modification of a read-only value attempted
>
>
> How can I get the secondary substitution to work only on a found regex?
> Or should I try another approach?


$ perl -e'
my $slurped_text = <<XML;

<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file">
<unitid>13</unitid>
<unittitle>
<unitdate>1928</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>14</unitid>
<unittitle>
<unitdate>1929</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>15</unitid>
<unittitle>
<unitdate>1930</unitdate>
</unittitle>
</c03>
</c02>

XML


print $slurped_text;
$slurped_text =~ s{(<(c0[1-9]|c1[012]) level="file">.*?</\2>)}{ ( my $x = $1 )
=~ s!\n *!!g; $x }seg;
print $slurped_text;
'

<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03 level="file">
<unitid>13</unitid>
<unittitle>
<unitdate>1928</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>14</unitid>
<unittitle>
<unitdate>1929</unitdate>
</unittitle>
</c03>
<c03 level="file">
<unitid>15</unitid>
<unittitle>
<unitdate>1930</unitdate>
</unittitle>
</c03>
</c02>


<c02 level="group">
<did>
<unitid>13-16</unitid>
<unittitle>several weeklies</unittitle>
<unitdate normal="1928/1931">1928-1931</unitdate>
<unitid>13-16</unitid>
</did>
<c03
level="file"><unitid>13</unitid><unittitle><unitdate>1928</unitdate></unittitle></c03>
<c03
level="file"><unitid>14</unitid><unittitle><unitdate>1929</unitdate></unittitle></c03>
<c03
level="file"><unitid>15</unitid><unittitle><unitdate>1930</unitdate></unittitle></c03>
</c02>




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gsub pattern substitution and ${...} Sarah Allen Ruby 7 05-11-2009 01:00 PM
Pattern based substitution Daniel Mircea Ruby 0 10-16-2006 05:17 PM
Regular Expression for pattern substitution Vibha Tripathi Python 3 07-01-2005 06:31 PM
utf8 issue with substitution pattern Vorxion Perl Misc 0 04-19-2005 01:30 AM
speed issues with pattern matching and substitution Arturi Perl Misc 8 04-05-2005 10:19 AM



Advertisments