Link to home
Start Free TrialLog in
Avatar of steph84
steph84

asked on

VERY easy question:

$string="1234123412341234"
I want $string2="1234 1234 1234 1234" from string1 value.
Most efficient/shortly/etc.. way to do that??
Avatar of ozo
ozo
Flag of United States of America image

$string=~s/41/4 1/g;
#it's not obvious what you might want if
#$string='a ab abcd  abcde abcdef abcdefg abcdefgh abcdefghi'
#some possibilities include:
($string2=$string)=~s/(\S{4})\B/$1 /g;
#or
$string2=join' ',($string=~/(....)/gs;
Avatar of steph84
steph84

ASKER

In fact, it was obvious...for me!
I just wanted to make group of 4 chars or digits, separated by one blank space.
The last answer is exactly what I was looking for.
you should answer now so I could grade you!!

Avatar of steph84

ASKER

In fact, it was obvious...for me!
I just wanted to make group of 4 chars or digits, separated by one blank space.
The last answer is exactly what I was looking for.
you should answer now so I could grade you!!

$string="1234123412341234"
$string =~ s/4/4 /g;
That puts a space at the end of $string which may not have been wanted.

What if $string contains non-digits, or groups of less than 4 digits separated by spaces?
Should those spaces be removed before regrouping them in fours?
Avatar of steph84

ASKER

Sorry hedj, but points are for ozo....
Ozo, no worrying about $string, they'll never have sapce or non digit char.
Thanks for all possibilities, that's what I'm looking for when I ask a question (especially as easy as this one) on experts-exchange..
You should answer!
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial