Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messagesroemerm 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
Post Follow-up to this messageOn 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)
Post Follow-up to this messageTore 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.
Post Follow-up to this messageGunnar 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?
Post Follow-up to this messageOn 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/;
}
Post Follow-up to this messagesroemerm <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
Post Follow-up to this messagesroemerm 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
Post Follow-up to this messageWhitey 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
Post Follow-up to this messageQuoth 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.