replacement pattern

Discussion in 'Perl Misc' started by tom, Jun 23, 2005.

  1. tom

    tom Guest

    I have a problem like this: need to parse through an xml document with
    a lot of stringvalue entries.

    I need to truncate the stringvalue to 10 chars while keeping the first
    10 chars untouched.

    <stringvalue="abcdefghijk..."/>

    What search and replacement pattern can be constructed for task like
    this in perl?
    $templine =~ s/?/?/gi;

    Thanks in advance for the help.
    tom, Jun 23, 2005
    #1
    1. Advertising

  2. tom

    Lars Eighner Guest

    In our last episode,
    <>,
    the lovely and talented tom
    broadcast on comp.lang.perl.misc:

    > I have a problem like this: need to parse through an xml document with
    > a lot of stringvalue entries.


    > I need to truncate the stringvalue to 10 chars while keeping the first
    > 10 chars untouched.


    ><stringvalue="abcdefghijk..."/>


    > What search and replacement pattern can be constructed for task like
    > this in perl?
    > $templine =~ s/?/?/gi;


    Is there any particular reason you wouldn't use substr instead?
    (Such as maybe this is a homework problem.)


    > Thanks in advance for the help.


    --
    Lars Eighner http://www.larseighner.com/
    College: The fountains of knowledge, where everyone goes to drink.
    Lars Eighner, Jun 23, 2005
    #2
    1. Advertising

  3. tom

    John Bokma Guest

    "tom" <> wrote:

    > I have a problem like this: need to parse through an xml document with
    > a lot of stringvalue entries.
    >
    > I need to truncate the stringvalue to 10 chars while keeping the first
    > 10 chars untouched.
    >
    > <stringvalue="abcdefghijk..."/>
    >
    > What search and replacement pattern can be constructed for task like
    > this in perl?


    Uhm: XML::parser + substr? + something that can output XML nice
    (XML::Writer? not sure about the name).

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jun 23, 2005
    #3
  4. tom

    Greg Bacon Guest

    In article <>,
    tom <> wrote:

    : I have a problem like this: need to parse through an xml document with
    : a lot of stringvalue entries.
    :
    : I need to truncate the stringvalue to 10 chars while keeping the first
    : 10 chars untouched.
    :
    : <stringvalue="abcdefghijk..."/>

    I'm confused about the output you want. Do you mean the result should
    be thirteen characters or ten? I assume you want to truncate the
    stringvalue to ten characters, showing that a truncation took place,
    and that only the first *seven* characters survive:

    $ cat try
    #! /usr/local/bin/perl

    while (<DATA>) {
    s!<stringvalue="(.{7}).{4,}"/>!<stringvalue="$1..."/>!;
    print;
    }

    __DATA__
    <stringvalue=""/>
    <stringvalue="123"/>
    <stringvalue="123456789A"/>
    <stringvalue="123456789AB"/>
    <stringvalue="abcdefghijk..."/>

    $ ./try
    <stringvalue=""/>
    <stringvalue="123"/>
    <stringvalue="123456789A"/>
    <stringvalue="1234567..."/>
    <stringvalue="abcdefg..."/>

    Hope this helps,
    Greg
    --
    This is the great illusion of our age, the idea that a certain class of
    people [i.e., government] is exempt from the moral judgments that apply
    to the rest of us.
    -- Gene Callahan
    Greg Bacon, Jun 23, 2005
    #4
    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. Jarkko Viinamäki
    Replies:
    1
    Views:
    4,171
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    Feb 22, 2004
  2. Replies:
    17
    Views:
    1,860
    Chris Uppal
    Nov 16, 2005
  3. Replies:
    9
    Views:
    5,321
    Lasse Reichstein Nielsen
    Mar 12, 2006
  4. tom

    pattern replacement in xml

    tom, Jun 21, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    156
    John Bokma
    Jun 22, 2005
  5. Replies:
    11
    Views:
    188
    Tad McClellan
    Apr 16, 2006
Loading...

Share This Page