First of all, it helps to have some decent code to test with; several inputs, and your various solutions for converting them.
foreach my $string (
'i am the very model of a modern major-general.',
'this is one sentence. this is two.',
'foo bar',
'outside parens (inside them)',
'outside brackets [inside them], now braces {inside}',
) {
(my $two_capture = $string) =~ s/ (^|[-\s(]) (\w) / $1 . uc $2 /e
+gx;
(my $boundary = $string) =~ s/ \b (\w) / uc $1 /e
+gx;
(my $alternation = $string) =~ s/ (-|\s|^|\() (. ) / $1 . uc $2 /e
+gx;
print(
"string: $string\n",
"two_capture: $two_capture\n",
"boundary: $boundary\n",
"alternation: $alternation\n",
"\n"
);
}
While it's good you defined what you consider a word, it's probably a little too narrow. Consider the bracket and brace examples, and the ease of simply using \b, rather than defining every character you consider a word delimiter. Also, (.) is probably wrong; you want word characters you can upcase (\w), not just any character.
Anyways, as the code shows, your attempts work just fine. What problem did you encounter when you tested them that made you think they didn't?
|