homepage Welcome to WebmasterWorld Guest from 23.20.61.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Inserting a line break into the Replacment string.
Davo1977

5+ Year Member



 
Msg#: 3712788 posted 10:40 am on Aug 1, 2008 (gmt 0)

I want to use a substitution regular expression to split words that are separated by commas onto separate lines. Say for example I wanted to split these words as follows -

Donna, 24 years of age, born Ipswich at 8.00pm,


Donna,
24 years of age,
born Ipswich at 8.00pm,

The first bit of code will read

my $text = $ARGV[0]
$text =~ s/[a-z],\n/i;
print $text. "/n";

I am trying to replace text with commas and put them into a list like above.

Can anyone help me with this script or suggest any better ideas?

 

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3712788 posted 3:20 pm on Aug 1, 2008 (gmt 0)

Can you just search on the comma (followed by a space) and replace that with the very same followed by a newline?
$text =~ s/, /, \n/g;

perl_diver

5+ Year Member



 
Msg#: 3712788 posted 7:46 pm on Aug 1, 2008 (gmt 0)

Your code has errors, this is not a substitution regexp:

$text =~ s/[a-z],\n/i;

the above regexp should throw an error.

and the line below is not printing a newline:

print $text . "/n";

This is waht you want:



$ARGV[0] = 'Donna, 24 years of age, born Ipswich at 8.00pm,';
my $text = $ARGV[0];
$text =~ s/([a-z], )/$1\n/gi;
print "$text\n";


Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved