Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Insert Space between names

by pg (Canon)
on Nov 21, 2005 at 01:43 UTC ( [id://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 (Saint) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://510334]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found