For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2005 > how to add a space using a regex









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to add a space using a regex
Jerry Preston

2005-08-31, 3:58 am

What I want to do is add a space between one's name when the are like the
following:

$name1 = "FirstLast:";

I want change $name1 = "First Last";

and

$name1 = "FirstMiddleLast:";

I want change $name1 = "First Middle Last";

To be able to add spaces for either "FirstLast:" or "FirstMiddleLast:" with
the same regex?

Thanks,

Jerry


Christian Winter

2005-08-31, 3:58 am

Jerry Preston wrote:
> What I want to do is add a space between one's name when the are like the
> following:
>
> $name1 = "FirstLast:";
>
> I want change $name1 = "First Last";
>
> and
>
> $name1 = "FirstMiddleLast:";
>
> I want change $name1 = "First Middle Last";
>
> To be able to add spaces for either "FirstLast:" or "FirstMiddleLast:" with
> the same regex?


You can use zero width look-behind and look-ahead assertions for
that.

In words:
Look for a place that is preceded by a lower case letter and
followed by an upper case letter and substitute that place with
a space.

Which looks like the following:
$name1 =~ s/(?<=[a-z])(?=[A-Z])/ /g;

But you should have in mind that your method isn't fool proof.
There may be surnames that contain more than one upper case letter
(just ask Tad ;-)).

See "perldoc perlre" for details on zero width assertions.

HTH
-Chris
William James

2005-08-31, 3:58 am


Jerry Preston wrote:
> What I want to do is add a space between one's name when the are like the
> following:
>
> $name1 = "FirstLast:";
>
> I want change $name1 = "First Last";
>
> and
>
> $name1 = "FirstMiddleLast:";
>
> I want change $name1 = "First Middle Last";
>
> To be able to add spaces for either "FirstLast:" or "FirstMiddleLast:" with
> the same regex?


In Ruby:

name1.gsub!(/(.)([A-Z])/,'\1 \2')

John Bokma

2005-08-31, 3:58 am

"Jerry Preston" <g-preston1@ti.com> wrote:

> What I want to do is add a space between one's name when the are like
> the following:
>
> $name1 = "FirstLast:";
>
> I want change $name1 = "First Last";
>
> and
>
> $name1 = "FirstMiddleLast:";
>
> I want change $name1 = "First Middle Last";
>
> To be able to add spaces for either "FirstLast:" or "FirstMiddleLast:"
> with the same regex?


s/([a-z])([A-Z])/$1 $2/g;

(Wonders about Old McDonald)

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Anno Siegel

2005-08-31, 3:58 am

Jerry Preston <g-preston1@ti.com> wrote in comp.lang.perl.misc:
> What I want to do is add a space between one's name when the are like the
> following:
>
> $name1 = "FirstLast:";
>
> I want change $name1 = "First Last";
>
> and
>
> $name1 = "FirstMiddleLast:";
>
> I want change $name1 = "First Middle Last";
>
> To be able to add spaces for either "FirstLast:" or "FirstMiddleLast:" with
> the same regex?


A regex doesn't change a string, but a substitution (one part of which
is a regex) does.

Can you insert spaces in either "FirstLast:" or "FirstMiddleLast:" with
the same substitution? That would depend on how you intend to recognize
the parts of the nname. You have said nothing about that.

Here is a substitution that inserts a space whereever a capital
letter immediately follows a lower-case one:

s/([[:lower:]])([[:upper:]])/$1 $2/g;

but that would mis-handle names like "McBarren".

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Bernard El-Hagin

2005-08-31, 3:58 am

"William James" <w_a_x_man@yahoo.com> wrote:

>
> Jerry Preston wrote:
>
> In Ruby:
>
> name1.gsub!(/(.)([A-Z])/,'\1 \2')



Why would you think a Ruby solution would be of interest here?


--
Cheers,
Bernard
Anno Siegel

2005-08-31, 3:58 am

Bernard El-Hagin <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in comp.lang.perl.misc:
> "William James" <w_a_x_man@yahoo.com> wrote:
>
>
>
> Why would you think a Ruby solution would be of interest here?


Troll troll -- they don't think much.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Anno Siegel

2005-08-31, 7:56 am

Bernard El-Hagin <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in comp.lang.perl.misc:
> "William James" <w_a_x_man@yahoo.com> wrote:
>
>
>
> Why would you think a Ruby solution would be of interest here?


Trolls troll -- they don't think much.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Bernard El-Hagin

2005-08-31, 7:56 am

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:

> Jerry Preston <g-preston1@ti.com> wrote in comp.lang.perl.misc:
>
> A regex doesn't change a string, but a substitution (one part of
> which is a regex) does.
>
> Can you insert spaces in either "FirstLast:" or "FirstMiddleLast:"
> with the same substitution? That would depend on how you intend
> to recognize the parts of the nname. You have said nothing about
> that.
>
> Here is a substitution that inserts a space whereever a capital
> letter immediately follows a lower-case one:
>
> s/([[:lower:]])([[:upper:]])/$1 $2/g;
>
> but that would mis-handle names like "McBarren".



I suggest that two letter first names are quite rare, which leads to
an obvious adjustment of your regex. Still not perfect, but much
closer.


--
Cheers,
Bernard
John Bokma

2005-08-31, 7:56 am

"Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote:

> I suggest that two letter first names are quite rare,


Not 2 letter first names, but for example TadMcClellan

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Bernard El-Hagin

2005-08-31, 7:56 am

John Bokma <john@castleamber.com> wrote:

> "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote:
>
>
> Not 2 letter first names, but for example TadMcClellan



What I mean is, you can safely assume that 2 letter names are too rare
to worry about, so adjusting the s/// to ignore two letter
[:upper:][:lower:] sequences should "fix" it.


--
Cheers,
Bernard
Anno Siegel

2005-08-31, 7:56 am

John Bokma <john@castleamber.com> wrote in comp.lang.perl.misc:
> "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote:
>
>
> Not 2 letter first names, but for example TadMcClellan


How is that an objection?

s/([[:alpha:]][[:lower:]])([[:upper:]])/$1 $2/g;

makes "Tad McClellan" as it should.

Ed, Al, Ty, Bo, Jo, Ma, Lu and Vi would be out of luck.

A US name list (from the Census Bureau) lists 6 male and 27(!) female
two-letter names.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Alan J. Flavell

2005-08-31, 7:56 am

On Wed, 31 Aug 2005, John Bokma wrote:

> (Wonders about Old McDonald)


Indeed. Then there are surnames like St. John.

This is addressed more to the O.P :-

Handling names is fraught, even if they're all written correctly!

Seems to me that trying to define some other format for writing them
is only going to make a difficult task even more difficult, maybe even
impossible. (I have a local script to deal with the names of members
of staff of the department that I'm in: and it's full of special
cases, and has to be updated several times a year. So I'm not talking
pure theory.)

Tad McClellan

2005-08-31, 7:56 am

John Bokma <john@castleamber.com> wrote:
> "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote:
>
>
> Not 2 letter first names, but for example TadMcClellan



I get junk mail addressed to: Mr. Clellan

That saves me from having to open the envelope. :-)


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Matt Garrish

2005-08-31, 6:57 pm


"Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in
message news:Xns96C372C42AA46elhber1lidotechnet@
62.89.127.66...
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>
>
>
> I suggest that two letter first names are quite rare, which leads to
> an obvious adjustment of your regex. Still not perfect, but much
> closer.
>


Discounting Asian surnames, which are often two letters and precede the
proper name...

And how about all the Eds and Als in the world?

Seems like a problem doomed to failure.

Matt


Bernard El-Hagin

2005-08-31, 6:57 pm

"Matt Garrish" <matthew.garrish@sympatico.ca> wrote:

[...]

>
> Discounting Asian surnames, which are often two letters and
> precede the proper name...



Yes, and what about "El-Hagin"? That's gotta be annoying.


> And how about all the Eds and Als in the world?



What about them?


> Seems like a problem doomed to failure.



I did say "Still not perfect", you know.


--
Cheers,
Bernard
Matt Garrish

2005-08-31, 6:57 pm


"Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in
message news:Xns96C39C786F50Felhber1lidotechnet@
62.89.127.66...
> "Matt Garrish" <matthew.garrish@sympatico.ca> wrote:
>
> [...]
>
>
>
> Yes, and what about "El-Hagin"? That's gotta be annoying.
>
>
>
>
> What about them?
>


Well, Ed was saying to Al that he was amazed so many people waste letters...
oh never mind. : )

Matt


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com