replacement pattern

T

tom

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

Lars Eighner

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.

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.)
 
J

John Bokma

tom said:
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).
 
G

Greg Bacon

: 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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top