removin \n from only part of a string

Discussion in 'Perl Misc' started by sroemerm, Oct 27, 2004.

  1. sroemerm

    sroemerm Guest

    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.
     
    sroemerm, Oct 27, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Oct 27, 2004
    #2
    1. Advertising

  3. sroemerm

    Tore Aursand Guest

    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 <>
    "Writing modules is easy. Naming modules is hard." (Anno Siegel, on
    comp.lang.perl.misc)
     
    Tore Aursand, Oct 28, 2004
    #3
  4. Tore Aursand <> writes:
    > 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 );


    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.
     
    Arndt Jonasson, Oct 28, 2004
    #4
  5. sroemerm

    sroemerm Guest

    Gunnar Hjalmarsson <> wrote in message news:<>...
    > 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;


    That works! Thanks, if you have time could you break that down and
    explain what is going on?
     
    sroemerm, Oct 28, 2004
    #5
  6. sroemerm

    Ben Morrow Guest

    Quoth (sroemerm):
    > Gunnar Hjalmarsson <> wrote in message news:<>...
    > > 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;

    >
    > 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, Oct 28, 2004
    #6
  7. 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/;
    }
     
    Whitey Johnson, Oct 28, 2004
    #7
  8. sroemerm <> wrote:
    > Gunnar Hjalmarsson <> wrote in message news:<>...
    >> sroemerm wrote:


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



    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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 28, 2004
    #8
  9. sroemerm wrote:
    > Gunnar Hjalmarsson wrote:
    >> sroemerm wrote:
    >>> 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///.)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 28, 2004
    #9
  10. 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
     
    Gunnar Hjalmarsson, Oct 28, 2004
    #10
  11. sroemerm

    sroemerm Guest

    > 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.
     
    sroemerm, Oct 29, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt

    Removin a user control

    Kurt, Jul 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    357
    Karl Seguin
    Jul 2, 2003
  2. Jack
    Replies:
    8
    Views:
    296
  3. diablo
    Replies:
    18
    Views:
    247
    Roland Hall
    Jul 4, 2005
  4. Zouplaz

    Keep only one part of a string

    Zouplaz, Sep 15, 2006, in forum: Ruby
    Replies:
    5
    Views:
    81
    Logan Capaldo
    Sep 15, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    637
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page