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

Insert Space between names

by steelrose (Scribe)
on Nov 21, 2005 at 01:36 UTC ( #510331=perlquestion: print w/ replies, xml ) Need Help??
steelrose has asked for the wisdom of the Perl Monks concerning the following question:

A simple one, I'm sure it's just my brain is fried at the moment.

I have a string, which will always contain a name in the format "JohnDoe", and I want to put a space between the names so it appears "John Doe".

thought I could do it simply by using a reg ex, but it appears to require a little more work than that.
$string =~ s/[A-Z]/ [A-Z]/g;
returns
[A-Z]ohn [A-Z]oe
Can a fellow monk help move me in the correct direction? Thanks.
If you give a man a fish he will eat for a day.
If you teach a man to fish he will buy an ugly hat.
If you talk about fish to a starving man, you're a consultant.

Comment on Insert Space between names
Select or Download Code
Re: Insert Space between names
by GrandFather (Cardinal) on Nov 21, 2005 at 01:42 UTC
    use strict; use Data::Dumper; my $name = 'JohnDoe, JaneAnneDoe'; $name =~ s/(?<=[a-z])(?=[A-Z])/ /g; print $name;

    Prints:

    John Doe, Jane Anne Doe

    DWIM is Perl's answer to Gödel
Re: Insert Space between names
by pg (Canon) on Nov 21, 2005 at 01:43 UTC

    One solution:

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

      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.

      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.

Re: Insert Space between names
by steveAZ98 (Monk) on Nov 21, 2005 at 02:02 UTC
    Another solution as long as your data conforms to the last name being capitalized.

    $name =~ s/([a-z])([A-Z])/$1 $2/;

    Handles the situation where you might have JohnMcDoe correctly.
Re: Insert Space between names
by davido (Archbishop) on Nov 21, 2005 at 03:50 UTC

    Or this...

    $string =~ s/(?<=\w)([A-Z])/ $1/g;

    Now any capital letter character preceedd by a 'word' character will have a space prepended.


    Dave

Re: Insert Space between names
by Tanktalus (Canon) on Nov 21, 2005 at 04:21 UTC

    How does your format show "Tina McBride" (anyone for some country music?) or "Tim O'Reilly" (mmmm... books...)? If they are "TinaMcBride" and "TimO'Reilly", then your algorithm will end up with "Tina Mc Bride" and "Tim O' Reilly". This is a HARD problem. Because some people are really, really picky about their names, and getting it wrong is just not a (legal) option.

    I had a coworker who legally appended his nickname to his name to make a new surname. That's because he lived in Toronto, but had a very Polish name - one with sounds that us poor English-speakers couldn't distinguish. He went by this nickname at all times, and wanted things like his email (centrally controlled via Lotus Notes) to show up as just his nickname - not his full name as is the default. It took him 6 months to get that changed. Because it was his legal surname, he had that legal right to be referred to by that name.

    It is that serious to some people.

Re: Insert Space between names
by Aristotle (Chancellor) on Nov 21, 2005 at 09:32 UTC
    s/\B(?=\p{Upper})/ /g;

    Don’t use [A-Z], that’s a red flag. It only works for English, at most.

    Makeshifts last the longest.

      Donít use [A-Z], thatís a red flag. It only works for English, at most.

      There is a lot of software that only works for Englsh; I'm not sure I'd call it a "red flag". Maybe more flexibility should be used in this case, and maybe it doesn't matter. Don't get me wrong... it's good advice. I just don't see a "red flag" when I see [A-Z] unless I know that the program needs to deal with character sets other than ASCII. That has been rare in my world (though decreasingly so.)

      -sauoq
      "My two cents aren't worth a dime.";
      

        Considering you don’t speak English, German and Greek, that doesn’t suprise me… :-)

        For me, it is the other way around: I use [A-Z] explicitly when I really mean to deal with ASCII only.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-12-25 01:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls