...

View Full Version : splitting a string



bazz
10-27-2005, 02:59 PM
Hi,

I have a string which I want to break up at each capital by putting a space immedately before each one.

ThisIsThursday should then read like this, This Is Thursday.

my ( $organisationName1 , $organisationName2, $organisationName3, $organisationName4, $organisationName5) = ($organisationName =~ /(....)/g); # split organisationName after each four characters.

Then I tried, $organisationName = split(/[A-Z]/); but that A-Z seems not to work

Can any of you give me an idea. Presently, I am thinking about 'split' then 'join' So any help with splitting on caps would be appreciated.


Bazz

FishMonger
10-27-2005, 03:31 PM
$organisationName =~ s/([^A-Z])([A-Z])/$1 $2/g;

bazz
10-27-2005, 05:53 PM
Thanks FishMonger.

I'm not sure I can see, readily, how that does it. I don't doubt you; I am just not able to understand it from a read through. I'll play with it and see.

Bazz

FishMonger
10-27-2005, 06:09 PM
The substitution regex looks for (matches) a capital letter preceeded by anything that is not a capital letter and puts the non capital characture into $1 and the capitol char into $2. It then replaces those 2 chars with themselves seperated by a space.

It can also be written as,

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

which alters it to make sure the preceeding char is a lowercase letter.

bazz
10-28-2005, 10:27 AM
I see it now since you wrote it the other way.

Thanks FishMonger.

Bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum