second substitution to work only on a found pattern

Discussion in 'Perl Misc' started by I.M. Postor, Oct 9, 2006.

  1. I.M. Postor

    I.M. Postor Guest

    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
     
    I.M. Postor, Oct 9, 2006
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Oct 9, 2006
    #2
    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. Vibha Tripathi

    Regular Expression for pattern substitution

    Vibha Tripathi, Jul 1, 2005, in forum: Python
    Replies:
    3
    Views:
    318
    Devan L
    Jul 1, 2005
  2. Daniel Mircea

    Pattern based substitution

    Daniel Mircea, Oct 16, 2006, in forum: Ruby
    Replies:
    0
    Views:
    83
    Daniel Mircea
    Oct 16, 2006
  3. Sarah Allen

    gsub pattern substitution and ${...}

    Sarah Allen, May 11, 2009, in forum: Ruby
    Replies:
    7
    Views:
    120
    Sarah Allen
    May 11, 2009
  4. Arturi
    Replies:
    8
    Views:
    129
    Anno Siegel
    Apr 5, 2005
  5. yelipolok
    Replies:
    4
    Views:
    273
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page