cooldood3490
cooldood3490

Reputation: 2498

How to insert space after every semicolon of a string in Perl?

I'm trying to insert a space after every semicolon of a string in Perl.

#!/usr/bin/perl
use strict; use warnings;
my $string = "1234;5678;232;5774;9784";
$string =~ s/;/"; "/g;
my $matched = $1;
print $matched . "\n";

But it doesn't work. My string is 1234;5678;232;5774;9784. I want to print 1234; 5678; 232; 5774; 9784. thanks

Upvotes: 0

Views: 150

Answers (1)

oalders
oalders

Reputation: 5279

You want to print $string not $matched. Also, you don't need the quotes in the regex, unless you want them in there as well.

#!/usr/bin/perl

use strict;
use warnings;

my $string = "1234;5678;232;5774;9784";
$string =~ s/;/; /g;
print "$string\n";

prints 1234; 5678; 232; 5774; 9784

Upvotes: 2

Related Questions