Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Insert Space between names

by pg (Canon)
on Nov 21, 2005 at 01:43 UTC ( #510334=note: print w/replies, xml ) Need Help??

in reply to Insert Space between names

One solution:

my $string = "JohnDoe"; $string =~ s/(.)([A-Z])/$1 $2/g; print $string;

Replies are listed 'Best First'.
Re^2: Insert Space between names
by GrandFather (Sage) on Nov 21, 2005 at 01:47 UTC

    Note that that puts a space in front of John.

    DWIM is Perl's answer to Gödel
      Depends on whether the string consists only of the name (or more precisely, starts with the name) or not. If it does then there's no extra space.

        Following the stealth update that is true. When I commented, the code was:

        my $string = "JohnDoe"; $string =~ s/([A-Z])/ $1/g; print $string;

        DWIM is Perl's answer to Gödel
Re^2: Insert Space between names
by Aristotle (Chancellor) on Nov 21, 2005 at 09:30 UTC

    You could fix your original code less invasively with

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

    But [A-Z] only works for English, at most, and should be avoided.

    s/(?<!^)(\p{Upper})/ $1/g;

    Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://510334]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2017-08-23 02:41 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (345 votes). Check out past polls.