Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VERY easy question:

Posted on 1998-10-13
8
Medium Priority
?
172 Views
Last Modified: 2010-03-05
$string="1234123412341234"
I want $string2="1234 1234 1234 1234" from string1 value.
Most efficient/shortly/etc.. way to do that??
0
Comment
Question by:steph84
  • 4
  • 3
8 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 1205311
$string=~s/41/4 1/g;
0
 
LVL 85

Expert Comment

by:ozo
ID: 1205312
#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;
0
 

Author Comment

by:steph84
ID: 1205313
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!!

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:steph84
ID: 1205314
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!!

0
 

Expert Comment

by:hedj
ID: 1205315
$string="1234123412341234"
$string =~ s/4/4 /g;
0
 
LVL 85

Expert Comment

by:ozo
ID: 1205316
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?
0
 

Author Comment

by:steph84
ID: 1205317
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!
0
 
LVL 85

Accepted Solution

by:
ozo earned 200 total points
ID: 1205318
$string2=join' ',$string=~/(.{1,4})/g;
#don't need the /s if you have only digits,
#{1,4} in case there's not enough digits to fill the last group of 4
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question