Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to find the last "new line" in string

Reply
Thread Tools

how to find the last "new line" in string

 
 
Walter Roberson
Guest
Posts: n/a
 
      02-07-2004
In article <c02egr$5d2$(E-Mail Removed)>, gnari <(E-Mail Removed)> wrote:
:"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> wrote in message
:news:c02026$j7v$(E-Mail Removed)...

:[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
 
Reply With Quote
 
 
 
 
Charles DeRykus
Guest
Posts: n/a
 
      02-07-2004
In article <c02026$j7v$(E-Mail Removed)>,
Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> wrote:
>In article <(E-Mail Removed)>,
>Charles DeRykus <(E-Mail Removed)> wrote:
>id 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
use file::find to find files modified in last 5 days STD Perl Misc 8 02-26-2012 06:52 PM
How to replace the last (and only last) character in a string? Johny Python 8 05-03-2007 09:24 PM
string find last of problem kaferro@hotmail.com C++ 3 04-05-2007 11:10 AM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM



Advertisments