removin \n from only part of a string

S

sroemerm

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

Gunnar Hjalmarsson

sroemerm said:
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;
 
A

Arndt Jonasson

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

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

sroemerm

Gunnar Hjalmarsson said:
sroemerm said:
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;

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

Ben Morrow

Quoth (e-mail address removed) (sroemerm):
Gunnar Hjalmarsson said:
sroemerm said:
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;

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
 
W

Whitey Johnson

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/;
}
 
T

Tad McClellan

sroemerm said:
Gunnar Hjalmarsson said:
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?


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

Gunnar Hjalmarsson

sroemerm said:
Gunnar said:
sroemerm said:
I want to get rid of only the new lines in between < and >

$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?

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///.)
 
G

Gunnar Hjalmarsson

Whitey said:
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 .).
 
S

sroemerm

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.
 

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,053
Latest member
BrodieSola

Latest Threads

Top