For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2004 > removin \n from only part of a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author removin \n from only part of a string
sroemerm

2004-10-27, 8:56 pm

I have a rather large string wiht all kinds of valid \n(s) but I have
some that I want to get rid of.

In the following example I want to get rid of only the new lines in
between < and >

my $string = "I like this\n but <I dont like\n the\n ones\n in here>"
I want the previous string to look like this
"I like this\n but <I dont like the ones in here>"

I've tried a couple things but the bottom line is that I'm not good at
expression parsing. please help.
Gunnar Hjalmarsson

2004-10-27, 8:56 pm

sroemerm wrote:
> I have a rather large string wiht all kinds of valid \n(s) but I have
> some that I want to get rid of.
>
> In the following example I want to get rid of only the new lines in
> between < and >
>
> my $string = "I like this\n but <I dont like\n the\n ones\n in here>"
> I want the previous string to look like this
> "I like this\n but <I dont like the ones in here>"
>
> I've tried a couple things but the bottom line is that I'm not good at
> expression parsing. please help.


$string =~ s/(<[^>]+> )/ ( my $tmp = $1 ) =~ s!\n!!g; $tmp /eg;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Tore Aursand

2004-10-28, 8:56 am

On Wed, 27 Oct 2004 23:12:50 +0200, Gunnar Hjalmarsson wrote:
> $string =~ s/(<[^>]+> )/ ( my $tmp = $1 ) =~ s!\n!!g; $tmp /eg;


How about this one?

1 while ( $string =~ s,(<.*?)\n(.*?> ),$1$2,sg );


--
Tore Aursand <toreau@gmail.com>
"Writing modules is easy. Naming modules is hard." (Anno Siegel, on
comp.lang.perl.misc)
Arndt Jonasson

2004-10-28, 8:57 am


Tore Aursand <toreau@gmail.com> writes:
> On Wed, 27 Oct 2004 23:12:50 +0200, Gunnar Hjalmarsson wrote:
>
> How about this one?
>
> 1 while ( $string =~ s,(<.*?)\n(.*?> ),$1$2,sg );


It fails if there are several <> groupings in the string. Try
$string = "A\nB\n< one\nline\n>\nC\nD<one\nmore\nline\n>\nE\n";

You could say it ignores the "^>" part in the other solution.
sroemerm

2004-10-28, 3:59 pm

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2uai68F28uim5U1@uni-berlin.de>...
> sroemerm wrote:
>
> $string =~ s/(<[^>]+> )/ ( my $tmp = $1 ) =~ s!\n!!g; $tmp /eg;


That works! Thanks, if you have time could you break that down and
explain what is going on?
Whitey Johnson

2004-10-28, 3:59 pm

On Wed, 27 Oct 2004 14:02:30 -0700, sroemerm muttered incoherently:

> I have a rather large string wiht all kinds of valid \n(s) but I have
> some that I want to get rid of.
>
> In the following example I want to get rid of only the new lines in
> between < and >
>
> my $string = "I like this\n but <I dont like\n the\n ones\n in here>"
> I want the previous string to look like this
> "I like this\n but <I dont like the ones in here>"
>
> I've tried a couple things but the bottom line is that I'm not good at
> expression parsing. please help.


how about this one:
while ($string =~ /<.*\n/) {
$string =~ s/(<.*)\n/$1/;
}

Tad McClellan

2004-10-28, 3:59 pm

sroemerm <roemerman@gmail.com> wrote:
> Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2uai68F28uim5U1@uni-berlin.de>...
[color=darkred]

[color=darkred]
>
> That works! Thanks, if you have time could you break that down and
> explain what is going on?



Untested, but should be executable same as above:

$string =~ s/( # start remembering characters
< # open angle bracket
[^>]+ # any chars that are not a closing angle bracket
> # close angle bracket

)
/ ( my $tmp = $1 ) =~ s!\n!!g; # the copy-and-modify idiom
$tmp # evaluated last so this is the code's return value
/egx;

The "copy-and-modify idiom" makes a copy and then modifies the copy,
leaving the original intact. Roughly equivalent to:

my $tmp = $1;
$tmp =~ s!\n!!g;

(needed here because $1 is read-only, we're not allowed to modify it.)

s///e changes the meaning of the replacement part. It is no longer a
string, now it is Perl code to evaluate. Whatever the code evaluates
to is used as the replacement string.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Gunnar Hjalmarsson

2004-10-28, 3:59 pm

sroemerm wrote:
> Gunnar Hjalmarsson wrote:
>
> That works! Thanks, if you have time could you break that down and
> explain what is going on?


Well, I used the s/// operator, which is explained in "perldoc perlop".
The pattern is replaced with the return value from the expression to the
right.

Try to figure things out yourself by studying the mentioned part of the
docs, and I'll be happy to answer possible *specific* questions you may
have.

One observation I make right now is that it would have been a better
choice to exchange

s!\n!!g

for

tr!\n!!d

They do the same thing, but the latter is probably more efficient. (The
tr/// operator is also described in "perldoc perlop", right after s///.)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson

2004-10-28, 3:59 pm

Whitey Johnson wrote:
>
> how about this one:
> while ($string =~ /<.*\n/) {
> $string =~ s/(<.*)\n/$1/;
> }


Fails for the same reason as Tore's solution (and can be fixed in a
similar way, i.e. using [^>] instead of .).

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Ben Morrow

2004-10-28, 8:56 pm


Quoth roemerman@gmail.com (sroemerm):
> Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2uai68F28uim5U1@uni-berlin.de>...
>
> That works! Thanks, if you have time could you break that down and
> explain what is going on?


I've added /x to make things clearer.

$string =~ # over $string
s/( < [^>]+ > )/ # extract < up to the nearest > and replace with
# the result of evaluating
(my $tmp = $1) # assign what we just grabbed (the <...> ) to $tmp
=~ # and apply the next s/// to it
s! \n !!gx; # which says 'replace newlines with nothing'
$tmp # return the new value, to be used as the
# replacement
/exg; # g => all matches, x => allow whitespace in
# pattern, e => treat replacement part as a Perl
# expression

Ben

--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] ben@morrow.me.uk
sroemerm

2004-10-29, 3:57 pm

> Well, I used the s/// operator, which is explained in "perldoc perlop".
> The pattern is replaced with the return value from the expression to the
> right.


> Try to figure things out yourself by studying the mentioned part of the
> docs, and I'll be happy to answer possible *specific* questions you may
> have.
>
> One observation I make right now is that it would have been a better
> choice to exchange
>
> s!\n!!g
>
> for
>
> tr!\n!!d
>
> They do the same thing, but the latter is probably more efficient. (The
> tr/// operator is also described in "perldoc perlop", right after s///.)


In between "perldoc perlop", and the other posts I think I've got it
now. In general, I had a fairly good idea how it worked, but some of
the specifics escaped me and my crapy resources. My only Perl
resource is a crummy Osborne book. Specify I did not know what [^>]
did, also !, I could not find those in this book, but I'll probably
use perldoc more in the future instead.

Thanks guys for all your help.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com