Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VERY easy question:

Posted on 1998-10-13
8
Medium Priority
?
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

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

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 84

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 84

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

715 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