Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex to substitute newlines, it works, but not like I want it to

Status
Not open for further replies.

jollekox

Programmer
Feb 3, 2004
23
0
0
NO
I have a little Perl script that goes something like this:

#!/usr/bin/perl -w

$lkd = "line one\nline two\nline three";
$lkd =~ s/\n/<br>/g;
print "$lkd";


And it outputs something like this:

line one
<br>line two
<br>line tree
<br>


I thought s/\n/<br>/g; would swap all the newlines with html break? It seems to me that it does not replace the newlines? Or should I append another letter to the regex so that it can substitute newlines?

Thanks in advance for your help!

Best Regards,
Jollekox
 
try this...

$lkd = "line one\nline two\nline three";
$lkd =~ s/[\r\n]/<br>/g;
print "$lkd";


Kind Regards
Duncan
 
Duncan,

Your code seems to be working see below.

Code:
C:\Scripts\Perl>type replacement-help.pl
$lkd = "line one\nline two\nline three";
print "$lkd";
$lkd =~ s/\n/<br>/g;
print "$lkd";

C:\Scripts\Perl>replacement-help.pl
line one
line two
line threeline one<br>line two<br>line three
C:\Scripts\Perl>

Is it possible that it's just not displaying correctly on your system?




Thanks, Danzig
 
DANZIG - sorry... I don't understand. I know it works? I was trying to find out if jollekox was using a PC (carriage return / line -feed) or was having some strange problem with UNIX?

jollekox code is fine under UNIX


Kind Regards
Duncan
 
Well, I am running Mac OS X, and it works great here, but on my webhost, the output looks like the first example I posted.

But I have had some small problems with Perl on that host before,
so it is probably just that (like, I tried to do math on 2 different macs running two different versions of Perl, but the calculations on the webhost turned out wrong each time, so I think I will send an email to my provider).

Strange problem, but the result looks good in the browser,
so I don't really care ;)

Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top