String Manipulation

D

dao

Hi, I am kina a perl-newbie and have both the learning perl book an the
programming perl book, but can't find the information I'm looking for
in either of them. What I need to do is as follows:

I have a string: AaaSmmmRiiiW

And I need to recognize te capital letters and turn the string into the
following: Aaa Smmm Riii W

Basically, every Capital needs to be preceeded by a space. I can't
figure out how to do it in perl.

I lloked at the FAQ at perl.org and came up empty there as well. I know
it can be done, I just can't figure it out.

Any help appreciated.


Thanks, Jack
 
D

Dr.Ruud

Mirco Wahab schreef:
dao:
I have a string: AaaSmmmRiiiW

And I need to recognize te capital letters and turn the string into
the following: Aaa Smmm Riii W

Basically, every Capital needs to be preceeded by a space. I can't
figure out how to do it in perl.


You could use a regular expression substitution:

my $str = 'AaaSmmmRiiiW';

$str =~ s/(?<!^)([A-Z])/ $1/g;

Variant without capturing:

perl -wle'
$_="AaaSmmmRiiiW";

s/(?<!^)(?=[A-Z])/ /g;

print
'
 
J

John W. Krahn

Hi, I am kina a perl-newbie and have both the learning perl book an the
programming perl book, but can't find the information I'm looking for
in either of them. What I need to do is as follows:

I have a string: AaaSmmmRiiiW

And I need to recognize te capital letters and turn the string into the
following: Aaa Smmm Riii W

Basically, every Capital needs to be preceeded by a space. I can't
figure out how to do it in perl.

I lloked at the FAQ at perl.org and came up empty there as well. I know
it can be done, I just can't figure it out.

Any help appreciated.

$ perl -le'$_ = q[AaaSmmmRiiiW]; print; s/(?=[[:upper:]])(?!\A)/ /g; print;'
AaaSmmmRiiiW
Aaa Smmm Riii W




John
 
T

Tad McClellan

I have a string: AaaSmmmRiiiW

And I need to recognize te capital letters and turn the string into the
following: Aaa Smmm Riii W

Basically, every Capital needs to be preceeded by a space.


Or, every run of lower case chars need to be succeeded by a space:


s/([a-z]+)/$1 /g;
 
A

anno4000

Hi, I am kina a perl-newbie and have both the learning perl book an the
programming perl book, but can't find the information I'm looking for
in either of them. What I need to do is as follows:

I have a string: AaaSmmmRiiiW

And I need to recognize te capital letters and turn the string into the
following: Aaa Smmm Riii W

Basically, every Capital needs to be preceeded by a space. I can't
figure out how to do it in perl.

I lloked at the FAQ at perl.org and came up empty there as well. I know
it can be done, I just can't figure it out.

Any help appreciated.


Thanks, Jack

....or split the string before each capital, then join it together with
blanks:

join ' ', split /(?=[[:upper:]])/;

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top