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

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;


Comment on Re: Insert Space between names
Download Code
Re^2: Insert Space between names
by GrandFather (Cardinal) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://510334]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-10-24 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls