Welcome to WebmasterWorld Guest from

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)

New User

5+ Year Member

joined:June 25, 2008
posts: 24
votes: 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,

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

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

Preferred Member

10+ Year Member

joined:Jan 5, 2006
votes: 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";