http://www.perlmonks.org?node_id=510334


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.