how to find the last "new line" in string

Discussion in 'Perl Misc' started by hshen, Feb 4, 2004.

  1. hshen

    hshen Guest

    Hi,
    If a string contains a few lines, (separated by '\n'), how can I make
    a regular expression to replace this '\n' by a space?
    Thanks!
    hshen, Feb 4, 2004
    #1
    1. Advertising

  2. hshen

    Matija Papec Guest

    X-Ftn-To: hshen

    (hshen) wrote:
    >Hi,
    >If a string contains a few lines, (separated by '\n'), how can I make
    >a regular expression to replace this '\n' by a space?


    s/\n$/ /;
    this is only for last newline


    --
    Matija
    Matija Papec, Feb 4, 2004
    #2
    1. Advertising

  3. hshen

    J Krugman Guest

    In <> (hshen) writes:

    >Hi,
    >If a string contains a few lines, (separated by '\n'), how can I make
    >a regular expression to replace this '\n' by a space?


    This will work even if the string doesn't end in a newline:

    s/^(.*)\n/$1 /s

    or

    s/\n(?=[^\n]*$)/ /

    jill
    J Krugman, Feb 4, 2004
    #3
  4. hshen

    Matija Papec Guest

    >>Hi,
    >>If a string contains a few lines, (separated by '\n'), how can I make
    >>a regular expression to replace this '\n' by a space?

    >
    >s/\n$/ /;
    >this is only for last newline


    chomp and $_.= ' ' for $string;

    I think this could be faster.



    --
    Matija
    Matija Papec, Feb 4, 2004
    #4
  5. hshen wrote:
    >
    > If a string contains a few lines, (separated by '\n'), how can I make
    > a regular expression to replace this '\n' by a space?


    You don't really need a regular expression.

    $string =~ tr/\n/ /;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Feb 6, 2004
    #5
  6. In article <>, John W. Krahn <> wrote:
    :hshen wrote:

    :> If a string contains a few lines, (separated by '\n'), how can I make
    :> a regular expression to replace this '\n' by a space?

    :You don't really need a regular expression.

    :$string =~ tr/\n/ /;

    But the Subject refers to the *last* "new line" in the string;
    your proposed solution would change *all* of them.
    --
    "WHEN QUINED, YIELDS A TORTOISE'S LOVE-SONG"
    WHEN QUINED, YIELDS A TORTOISE'S LOVE-SONG. (GEB)
    Walter Roberson, Feb 6, 2004
    #6
  7. Walter Roberson wrote:
    >
    > In article <>, John W. Krahn <> wrote:
    > :hshen wrote:
    >
    > :> If a string contains a few lines, (separated by '\n'), how can I make
    > :> a regular expression to replace this '\n' by a space?
    >
    > :You don't really need a regular expression.
    >
    > :$string =~ tr/\n/ /;
    >
    > But the Subject refers to the *last* "new line" in the string;
    > your proposed solution would change *all* of them.


    Yah, I hate when they put the important information in the subject line
    instead of the actual message. :)

    $_ = reverse $string;
    s/\n/ /;
    $string = reverse $_;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Feb 6, 2004
    #7
  8. In article <>, John W. Krahn <> wrote:
    >Walter Roberson wrote:
    >>
    >> In article <>, John W. Krahn <> wrote:
    >> :hshen wrote:
    >>
    >> :> If a string contains a few lines, (separated by '\n'), how can I make
    >> :> a regular expression to replace this '\n' by a space?
    >>
    >> :You don't really need a regular expression.
    >>
    >> :$string =~ tr/\n/ /;
    >>
    >> But the Subject refers to the *last* "new line" in the string;
    >> your proposed solution would change *all* of them.

    >
    >Yah, I hate when they put the important information in the subject line
    >instead of the actual message. :)
    >
    >$_ = reverse $string;
    >s/\n/ /;
    >$string = reverse $_;
    >


    Did someone already mention: $string =~ s/\n\Z//;



    --
    Charles DeRykus
    Charles DeRykus, Feb 7, 2004
    #8
  9. In article <>,
    Charles DeRykus <> wrote:
    :Did someone already mention: $string =~ s/\n\Z//;

    That's wrong in three ways:

    \Z Match only at end of string, or before newline at the end
    \z Match only at end of string

    Bug #1: If the newline is the last character of the string,
    \n\Z is going to want to match a newline -before- that.

    Bug #2: The poster wanted to replace the last newline with a space,
    not remove it.

    Bug #3: If the last newline in the string is not at the end of the
    string, the pattern won't match it.

    This should work, though (I think):

    $string =~ s/\n([^\n]*)\z/ \1/;
    --
    "There are three kinds of lies: lies, damn lies, and statistics."
    -- not Twain, perhaps Disraeli, first quoted by Leonard Courtney
    Walter Roberson, Feb 7, 2004
    #9
  10. hshen

    gnari Guest

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:c02026$j7v$...

    [replacing last newline in string to space]

    >
    > This should work, though (I think):
    >
    > $string =~ s/\n([^\n]*)\z/ \1/;


    why the [^\n] ?
    whats wrong with:

    $string =~ s/\n(.*)\z/ \1/;

    gnari
    gnari, Feb 7, 2004
    #10
  11. In article <c02egr$5d2$>, gnari <> wrote:
    :"Walter Roberson" <-cnrc.gc.ca> wrote in message
    :news:c02026$j7v$...

    :[replacing last newline in string to space]
    :> This should work, though (I think):

    :> $string =~ s/\n([^\n]*)\z/ \1/;

    :why the [^\n] ?
    :whats wrong with:

    :$string =~ s/\n(.*)\z/ \1/;

    Good point, . doesn't match newline unless you use /s
    I just tested a bit and what you suggest seems to do the job.
    --
    And the wind keeps blowing the angel / Backwards into the future /
    And this wind, this wind / Is called / Progress.
    -- Laurie Anderson
    Walter Roberson, Feb 7, 2004
    #11
  12. In article <c02026$j7v$>,
    Walter Roberson <-cnrc.gc.ca> wrote:
    >In article <>,
    >Charles DeRykus <> wrote:
    >:Did someone already mention: $string =~ s/\n\Z//;
    >
    >That's wrong in three ways:
    >
    > \Z Match only at end of string, or before newline at the end
    > \z Match only at end of string
    >
    >Bug #1: If the newline is the last character of the string,
    >\n\Z is going to want to match a newline -before- that.
    >
    >Bug #2: The poster wanted to replace the last newline with a space,
    >not remove it.
    >
    >Bug #3: If the last newline in the string is not at the end of the
    >string, the pattern won't match it.
    >
    >This should work, though (I think):
    >
    > $string =~ s/\n([^\n]*)\z/ \1/;


    Ah, right on all counts...

    Here's a shorter possibility (hopefully correct too :):

    $string =~ s/(.*)\n/$1 /s;

    --
    Charles DeRykus
    Charles DeRykus, Feb 7, 2004
    #12
    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. sangram
    Replies:
    16
    Views:
    1,978
  2. Replies:
    3
    Views:
    418
  3. Johny
    Replies:
    8
    Views:
    389
  4. Wybo Dekker
    Replies:
    1
    Views:
    350
    Yukihiro Matsumoto
    Nov 15, 2005
  5. STD
    Replies:
    8
    Views:
    615
    Randal L. Schwartz
    Feb 26, 2012
Loading...

Share This Page