how to find the last "new line" in string

H

hshen

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

Matija Papec

X-Ftn-To: hshen

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
 
J

J Krugman

In said:
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

John W. Krahn

hshen said:
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
 
W

Walter Roberson

: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.
 
J

John W. Krahn

Walter said:
: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
 
C

Charles DeRykus

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//;
 
W

Walter Roberson

: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/;
 
G

gnari

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

Walter Roberson

:
:[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.
 
C

Charles DeRykus

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top