Welcome to WebmasterWorld Guest from 54.198.69.193

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Inserting a line break into the Replacment string.

     
10:40 am on Aug 1, 2008 (gmt 0)

5+ Year Member



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?

3:20 pm on Aug 1, 2008 (gmt 0)

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



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;
7:46 pm on Aug 1, 2008 (gmt 0)

5+ Year Member



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month