how to add a space using a regex

J

Jerry Preston

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
 
W

William James

Jerry said:
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')
 
J

John Bokma

Jerry Preston said:
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)
 
A

Anno Siegel

Jerry Preston said:
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
 
A

Anno Siegel

Bernard El-Hagin said:
William James said:
Jerry said:
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')


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

Troll troll -- they don't think much.

Anno
 
A

Anno Siegel

Bernard El-Hagin said:
William James said:
Jerry said:
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')


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

Trolls troll -- they don't think much.

Anno
 
A

Anno Siegel

John Bokma said:
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
 
A

Alan J. Flavell

(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.)
 
T

Tad McClellan

John Bokma said:
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. :)
 
M

Matt Garrish

Bernard El-Hagin said:
Jerry Preston said:
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".


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
 
M

Matt Garrish

Bernard El-Hagin said:
[...]
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?

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

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top