
tacallah
New User
Feb 11, 2009, 6:31 AM
Post #1 of 5
(9286 views)
|
How to change [1,2,3] to [1][2][3]
|
Can't Post
|
|
I am trying to write a regular expression to do the following: [#,#,#] => [#][#][#] [ # , # , # ] => [#][#][#] Here is what I have tried: my $x = "[3,4,5]\n[ 3 , 4 , 5 ]\n"; print "BEFORE\n$x"; $x =~ s/(?<=\[)\s*(\d+)\s*,/$1][/g; print "AFTER\n$x"; >>> OUTPUT <<< BEFORE [3,4,5] [ 3 , 4 , 5 ] AFTER [3][4,5] [3][ 4 , 5 ] The following will work by using a while loop: my $x = "[3,4,5]\n[ 3 , 4 , 5 ]\n"; print "BEFORE\n$x"; while ($x =~ s/\[\s*(\d+)\s*,/[$1][/g) {} $x =~ s/\[\s*(\d+)\s*\]/[$1]/g; print "AFTER\n$x"; >>> OUTPUT <<< BEFORE [3,4,5] [ 3 , 4 , 5 ] AFTER [3][4][5] [3][4][5] Is there any way to this with a single substitute? Thanks!
|