From: John on 10 Jul 2010 05:38 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
From: Dr.Ruud on 10 Jul 2010 06:09 John wrote: > 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. -- Ruud
From: Don Piven on 10 Jul 2010 08:13 On 07/10/2010 04:38 AM, John wrote: > 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? What you could do in this case is to use the /e switch, which causes the right side to be eval'ed: $x =~ s| Mc([a-z]{1}) | "Mc".uc($1) |ex ; (Oh, fixed those other two bugs for ya :-) Don Piven
From: Tad McClellan on 10 Jul 2010 11:02 John <john1949(a)yahoo.com> wrote: > I need to use the uc function within the regex expression. You are NOT using the uc function within the regex expression below! Perl's operators that use regular expressions are documented in the "Regexp Quote-Like Operators" section in: perldoc perlop For the s/// operator it says: s/PATTERN/REPLACEMENT/msixpogce PATTERN is a regex, REPLACEMENT is a string (ie. not a regex). > $x=~ s| Mc([A-Z]{1})| Mcuc($1) |g; You are using the uc function in the _replacment_ part of the s/// operator. > The above clearly does not work. How do you insert a function in such an > expression? If you carefully read the documentation for the function, you will discover that you do not need a function. You can use a backslash escape to affect case. perldoc -f uc perldoc -f ucfirst 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... -- 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.
From: John on 11 Jul 2010 04:58 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
|
Next
|
Last
Pages: 1 2 Prev: FAQ 4.25 How do I expand tabs in a string? Next: FAQ 9.17 How do I check a valid mail address? |