Solved

VERY easy question:

Posted on 1998-10-13
8
163 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 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 50 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now