Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

removin \n from only part of a string
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.

Report this thread to moderator Post Follow-up to this message
Old Post
sroemerm
10-28-04 01:56 AM


Re: removin \n from only part of a string
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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
10-28-04 01:56 AM


Re: removin \n from only part of a string
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)

Report this thread to moderator Post Follow-up to this message
Old Post
Tore Aursand
10-28-04 01:56 PM


Re: removin \n from only part of a string
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Arndt Jonasson
10-28-04 01:57 PM


Re: removin \n from only part of a string
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?

Report this thread to moderator Post Follow-up to this message
Old Post
sroemerm
10-28-04 08:59 PM


Re: removin \n from only part of a string
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/;
}


Report this thread to moderator Post Follow-up to this message
Old Post
Whitey Johnson
10-28-04 08:59 PM


Re: removin \n from only part of a string
sroemerm <roemerman@gmail.com> wrote:
> Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2uai68F28uim
5U1@uni-berlin.de>... 
 

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

Report this thread to moderator Post Follow-up to this message
Old Post
Tad McClellan
10-28-04 08:59 PM


Re: removin \n from only part of a string
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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
10-28-04 08:59 PM


Re: removin \n from only part of a string
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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
10-28-04 08:59 PM


Re: removin \n from only part of a string
Quoth roemerman@gmail.com (sroemerm):
> Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2uai68F28uim
5U1@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

Report this thread to moderator Post Follow-up to this message
Old Post
Ben Morrow
10-29-04 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:05 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.