regex Mcalpine to McAlpine

J

John

Hi

I've reduced some text to upper and lower case.

Some names need uppercase within the word. Mcalpine would be McAlpine.

I need to use the uc function within the regex expression.

$x=~ s| Mc([A-Z]{1})| Mcuc($1) |g;

The above clearly does not work. How do you insert a function in such an
expression?

Regards
John
 
D

Dr.Ruud

John said:
I need to use the uc function within the regex expression.

$x=~ s| Mc([A-Z]{1})| Mcuc($1) |g;

The above clearly does not work. How do you insert a function in such an
expression?

That is an XY problem. http://www.google.co.uk/#hl=en&q=xy+problem


Look ma, no function:

s/(?<= Mc)([a-z])/\u$1/g;


To find out how to use a function though, checkout the e-modifier in
`perldoc perlre`. There is also perlretut.
 
J

John

Thank you gentlemen.

That was very useful.

I can use either \u without e modifer or uc($1) with e modifier.

Thanks for pointing out the [a-z] error.

I had realised machine and macro need to be checked. There are other less
important words that would be trapped.

Appereciate your input. Thank you.

Regards
John
 
S

sln

Quoth Tad McClellan said:
So then,

s/\bMc([a-z])/Mc\U$1/;
or
s/(?<=\bMc)([a-z])/\U$1/;


Good luck getting MacArthur from Macarthur without also getting
MacHine from Machine...

s/(?<=\bMac)(?!h)([a-z])/\U$1/;

obviously...
?? What about Machenderson ??
I think Mc... is a little easier to "divine" than Mac...
Either way, a list of valid names would have to be used.
The point is moot.

-sln
 
J

John

Quoth Tad McClellan said:
So then,

s/\bMc([a-z])/Mc\U$1/;
or
s/(?<=\bMc)([a-z])/\U$1/;


Good luck getting MacArthur from Macarthur without also getting
MacHine from Machine...

s/(?<=\bMac)(?!h)([a-z])/\U$1/;

obviously...
?? What about Machenderson ??
I think Mc... is a little easier to "divine" than Mac...
Either way, a list of valid names would have to be used.
The point is moot.

-sln

Hi

I just check for the following afterwards:

Mach Machete Machine Machinable Mackerel Macro Mace Macerate Macula

otherwise the replacement will be OK.

As you say, Mc, is easier.

Regards
John
 
J

John

Tad McClellan said:
John said:
Quoth Tad McClellan <[email protected]>:

So then,

s/\bMc([a-z])/Mc\U$1/;
or
s/(?<=\bMc)([a-z])/\U$1/;


Good luck getting MacArthur from Macarthur without also getting
MacHine from Machine...

s/(?<=\bMac)(?!h)([a-z])/\U$1/;

obviously...

?? What about Machenderson ??
I think Mc... is a little easier to "divine" than Mac...
Either way, a list of valid names would have to be used.
The point is moot.

-sln

Hi

I just check for the following afterwards:

Mach Machete Machine Machinable Mackerel Macro Mace Macerate Macula


There are 251 names that start with MAC at

http://names.mongabay.com/surnames_M.htm

A fair number of them should not be changed as you suggest..

MacHiavelli, MacIna, MacK, MacOmb etc...

otherwise the replacement will be OK.


OK with you maybe, but almost for sure not OK with the person
whose name you mangle. [1]





[1] I often get snail mail addressed to Mr. Clellan, I assume
because there's only a few pixels difference between "c"
and "r" which confused an OCR program somewhere...

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


There is a nice routine on Andy Kramek's blog. Google "Andy Kramek
checkscots".
I dare say some names will be mangled. Macey is the obvious one.
Give it to a first year student as an exercise is probably the best
approach.

Regards
John
 

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

No members online now.

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,706
Latest member
HFWTina07

Latest Threads

Top