Lets say you have a string where you want to ucfirst each word. You could do that with the following:
$string = s/\b(\w+)\b/ucfirst($1)/eg;
You could modify the above regexp to handle apostrophes and hyphens by using negative lookbehind:
$string = s/(?<![-'])\b(\w+)\b/ucfirst($1)/eg;
The above substitution regex will apply ucfirst to any cluster of "word" characters as long as they're not immediately preceeded by an apostrophe or a hyphen. That way you don't end up with Frank'S Place when you really want Frank's Place.
Negative lookbehind with a character class is the right tool, as opposed to positive lookbehind for a negated character class, because you only care that an apostrophe or hyphen doesn't appear before the 'word' cluster. A negated character class inside of a positive lookbehind would require that a "non-apostrophe, non-hyphen" exist in front of the word cluster, which is going to foul everything up. You don't want that. ;)
Dave
In reply to Re: change case
by davido
in thread change case
by texuser74
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |