Reputation:
What is the best way to insert a character into a string at multiple positions in Perl? I have a string in which I have removed any non-numeric characters. After that, I want to insert a dash at the 4th position and 8th position starting from the end. You guessed it, I'm modifying all inputted phone numbers so that they all have the same format: XXX-XXX-XXXX
$number =~ s/[^0-9]//g;
substr($number, -4, 0, '-');
substr($number, -8, 0, '-');
Is there a regex that can do the above code but in a single line? Is there something more efficient out there that I don't know about?
Thanks in advance!
Upvotes: 1
Views: 1318
Reputation: 29854
Not sure about performance, but unpack
-and-join
also works:
join( '-', unpack 'A3 A3 A4', $number )
Upvotes: 4